底部菜单栏(一)Fragment+FragmentTabHost 来实现底部菜单栏方式
来源:互联网 发布:2016淘宝会员名字大全 编辑:程序博客网 时间:2024/06/02 06:14
这是一种实现底部导航栏的方法(方便简洁),也可以自定义布局,进行click;因此Fragment在一段时间内被疯狂的使用~
源代码下载点:http://download.csdn.net/detail/cl18652469346/9608292
效果图在末尾·········
(1)修改主布局代码,加入了FragmentTabHost 组件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/realtabcontent" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/main_tab_item_bg"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost></LinearLayout>
(2)单独为Tab按钮选项布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:padding="3dp" android:src="@drawable/main_tab_item_home"> </ImageView> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" android:textColor="#ffffff"> </TextView></LinearLayout>
(3) fragment布局界面和之前一样,就不再赘述
(4) Tab选项的自定义按钮中图片资源文件,列出其中一个按钮,指定了按钮的选中状态和不选中状态不同的图片显示
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/main_tab_item_home_focus" android:state_selected="true"/> <item android:drawable="@drawable/main_tab_item_home_normal"/></selector>
(5) Tab选项按钮背景资源文件,指定了点击的效果
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- pressed --> <item android:drawable="@drawable/main_tab_item_bg_focus" android:state_enabled="true" android:state_pressed="true"/> <!-- focused --> <item android:drawable="@drawable/main_tab_item_bg_focus" android:state_enabled="true" android:state_focused="true"/> <!-- normal --> <item android:drawable="@drawable/main_tab_item_bg_normal" android:state_enabled="true"/></selector>
(6) 最后就是主界面代码的改变
package com.yanis.yc_ui_fragment_tabhost;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTabHost;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.TabHost.TabSpec;import android.widget.TextView;/** * * @author yechao * @功能说明 自定义TabHost * */public class MainActivity extends FragmentActivity { // 定义FragmentTabHost对象 private FragmentTabHost mTabHost; // 定义一个布局 private LayoutInflater layoutInflater; // 定义数组来存放Fragment界面 private Class fragmentArray[] = { FragmentHome.class, FragmentCategory.class, FragmentDown.class, FragmentUser.class, FragmentSetting.class }; // 定义数组来存放按钮图片 private int mImageViewArray[] = { R.drawable.main_tab_item_home, R.drawable.main_tab_item_category, R.drawable.main_tab_item_down, R.drawable.main_tab_item_user, R.drawable.main_tab_item_setting }; // Tab选项卡的文字 private String mTextviewArray[] = { "主页", "分类", "下载", "我的", "设置" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } /** * 初始化组件 */ private void initView() { // 实例化布局对象 layoutInflater = LayoutInflater.from(this); // 实例化TabHost对象,得到TabHost mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); // 得到fragment的个数 int count = fragmentArray.length; for (int i = 0; i < count; i++) { // 为每一个Tab按钮设置图标、文字和内容 TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]) .setIndicator(getTabItemView(i)); // 将Tab按钮添加进Tab选项卡中 mTabHost.addTab(tabSpec, fragmentArray[i], null); // 设置Tab按钮的背景 mTabHost.getTabWidget().getChildAt(i) .setBackgroundResource(R.drawable.main_tab_item_bg); } } /** * 给Tab按钮设置图标和文字 */ private View getTabItemView(int index) { View view = layoutInflater.inflate(R.layout.tab_item_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.imageview); imageView.setImageResource(mImageViewArray[index]); TextView textView = (TextView) view.findViewById(R.id.textview); textView.setText(mTextviewArray[index]); return view; }}
效果图:
2 0
- 底部菜单栏(一)Fragment+FragmentTabHost 来实现底部菜单栏方式
- FragmentTabHost+Fragment实现底部菜单栏
- Fragment+FragmentTabHost实现底部菜单栏
- Fragment+FragmentTabHost实现底部菜单栏
- FragmentTabHost+Fragment实现底部菜单栏
- 底部菜单栏(一)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- 【Android】底部菜单栏--Fragment+FragmentTabHost实现
- #Android笔记#fragment+fragmentTabHost实现底部菜单栏
- 用Fragment+FragmentTabHost++ViewPager 实现底部菜单栏
- Fragment+FragmentTabHost实现仿QQ底部菜单栏
- 底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- 底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- FragmentTabHost实现底部菜单栏效果~~
- Fragment实现底部菜单栏
- Fragment实现底部菜单栏
- Fragment实现底部菜单栏
- 底部菜单栏(二)使用Fragment实现底部菜单栏
- 底部菜单栏 Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- gvim 设置字体
- 为什么要用 Node.js
- Oracle 12c中使用透明网关(gateways) 12.1数据链接到SQL Server 2012
- ios大话设计模式-简单工厂
- ThinkPHP实战02——《ThinkPHP3.2.3 实战个人博客》笔记——引入ThinkPHP
- 底部菜单栏(一)Fragment+FragmentTabHost 来实现底部菜单栏方式
- hdu 1081 To the Max
- 扩展欧几里德算法求解线性同余方程
- JAVA基础——多线程
- halcon学习笔记(1)——halcon开发环境配置及其他
- 设计模式-4.观察者模式
- 多线程——改变线程的名字(setPriority(int)方法、toString()方法以及wait方法和sleep方法的异同点)
- 工作笔记-Datatable部分
- OC中UIKit框架结构图