您的位置:首页 > 其它

FragmentTabHost+FrameLayout实现底部菜单栏

2017-01-16 15:35 369 查看
现在一般的app都使用底部菜单栏,那具体怎么实现的呢!我们就来看看

首先给大家展示一下布局文件

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


接下来就是怎么使用了,其实比较简单,我们就看代码吧!

1 //数据
2 private int mImageViewArray[] = {R.drawable.home_tab1, R.drawable.home_tab2, R.drawable.home_tab3};
3     private String mTextviewArray[] = {"首页", "设置","我的"};
4     private Class fragmentArray[] = {Fragment1.class, Fragment2.class, Fragment3.class};
5
6 //初始化以及设置数据
7  mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
8         mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
9         int count = fragmentArray.length;
10         for (int i = 0; i < count; i++) {
11             TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
12                     .setIndicator(getTabItemView(i));
13             mTabHost.addTab(tabSpec, fragmentArray[i], null);
14             mTabHost.getTabWidget().getChildAt(i)
15                     .setBackgroundResource(R.drawable.bg_tbitem);
16         }
17         mTabHost.setCurrentTabByTag(mTextviewArray[0]);
18         mTabHost.getTabWidget().setDividerDrawable(null);
19
20         mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
21             @Override
22             public void onTabChanged(String s) {
23
24
25             }
26         });
27
28
29  /**
30      * 项的样式
31      *
32      * @param index 第几个
33      * @return 每一个Tab样式
34      */
35     private View getTabItemView(int index) {
36         View view = layoutInflater.inflate(R.layout.tab_home_item, null);
37         ImageView imageView = (ImageView) view.findViewById(R.id.icon);
38         imageView.setImageResource(mImageViewArray[index]);
39         TextView textView = (TextView) view.findViewById(R.id.name);
40         textView.setText(mTextviewArray[index]);
41         return view;
42     }


对了,其中图片我们都设置成了selector,才有点击变色的效果

1 <selector xmlns:android="http://schemas.android.com/apk/res/android">
2     <item android:drawable="@drawable/btn_jiance_pre" android:state_pressed="false" android:state_selected="true" />
3     <item android:drawable="@drawable/btn_jiance_nor" android:state_focused="false" android:state_pressed="false" android:state_selected="false" />
4     <item android:drawable="@drawable/btn_jiance_pre" android:state_focused="true" android:state_pressed="true" />
5 </selector>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐