首页底部菜单FragmentTabHost的使用

一般现在的菜单都是底部FragmentTabHost,切换Fragment来实现的,今天我们就使用这个来看看如何实现的

首先是布局文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5     <LinearLayout
 6         android:layout_width="match_parent"
 7         android:layout_height="match_parent"
 8         android:orientation="vertical">
 9         <FrameLayout
10             android:id="@+id/realtabcontent"
11             android:layout_width="fill_parent"
12             android:layout_height="0dip"
13             android:layout_weight="1"
14             android:background="@color/white" />
15 
16 
17         <LinearLayout
18             android:layout_width="match_parent"
19             android:layout_height="wrap_content"
20             android:layout_alignParentBottom="true"
21             android:orientation="vertical">
22 
23             <View
24                 android:layout_width="match_parent"
25                 android:layout_height="1px"
26                 android:background="@color/color_home_tab_line" />
27 
28             <android.support.v4.app.FragmentTabHost
29                 android:id="@android:id/tabhost"
30                 android:layout_width="fill_parent"
31                 android:layout_height="wrap_content"
32                 android:background="@color/et_divider_disable">
33 
34                 <FrameLayout
35                     android:id="@android:id/tabcontent"
36                     android:layout_width="0dp"
37                     android:layout_height="0dp"
38                     android:layout_weight="0" />
39             </android.support.v4.app.FragmentTabHost>
40         </LinearLayout>
41     </LinearLayout>
42   
43 </RelativeLayout>

再来看看主界面代码如何实现,我的每个步骤都有注释哦!

1.初始化参数

  @ViewInject(R.id.rl_center)
    private RelativeLayout rl_center;
    @ViewInject(android.R.id.tabhost)
    private FragmentTabHost mTabHost;
//底部菜单的图标
private int mImageViewArray[] = {R.drawable.home_tab1, R.drawable.home_tab2, R.drawable.home_centertab, R.drawable.home_tab3, R.drawable.home_tab4}; //底部菜单的标题
private String mTextviewArray[] = {"工作", "消息","签到","联系人","我的"};
//底部菜单对应的fragment
private Class fragmentArray[] = {Fragment1.class, Fragment2.class, Fragment3.class, Fragment3.class, Fragment4.class}; private LayoutInflater layoutInflater;

2.初始化底部工具栏

/**
     * 初始化底部工具栏
     */
    private void initTabHost() {
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        int count = fragmentArray.length;
        for (int i = 0; i < count; i++) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
                    .setIndicator(getTabItemView(i));//getTabItemView的方法
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
            mTabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.bg_tbitem);
        }
        mTabHost.setCurrentTabByTag(mTextviewArray[0]);//设置当前菜单tab
        mTabHost.getTabWidget().setDividerDrawable(null);

        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
              

            }
        });
    }

3.项的样式

 1   /**
 2      * 项的样式
 3      *
 4      * @param index 第几个
 5      * @return 每一个Tab样式
 6      */
 7     private View getTabItemView(int index) {
 8         View view = layoutInflater.inflate(R.layout.tab_home_item, null);
 9         ImageView imageView = (ImageView) view.findViewById(R.id.icon);
10         imageView.setImageResource(mImageViewArray[index]);
11         TextView textView = (TextView) view.findViewById(R.id.name);
12         textView.setText(mTextviewArray[index]);
13         return view;
14     }
原文地址:https://www.cnblogs.com/wangying222/p/6672084.html