FragmentTabHost

来源:互联网 发布:cpu锁帧软件 编辑:程序博客网 时间:2024/06/03 07:12

虽然经常使用FragmentTabHost却一直不太了解其内部原理,今天稍微看了一下源码,总结一下,后续再慢慢完善,如果有理解不对的地方,烦请留言告知!

一、FragmentTabHost的使用方式

1、编写布局xml

2、tabHost=findViewByID(R.id.fragment_tabHost

3、tabHost.setUp(context,FragmentManager,Real_content_FrameLayout_id);

4、initTab();

private void initTab() {    addFragment(Fragment1.class, null, R.string.tab1, R.drawable.ico1);    addFragment(Fragment2.class, null, R.string.tab2, R.drawable.ico2);    addFragment(Fragment3.class, null, R.string.tab3, R.drawable.ico3);    addFragment(Fragment4.class, null, R.string.tab4, R.drawable.ico4);    addFragment(Fragment5.class, null, R.string.tab5, R.drawable.ico5);}private void addFragment(Class<? extends Fragment> clz, View tab, int tagTvid, int tagImageId) {    String tag = clz.getName();  //这个不会显示在界面上,是tab的唯一标识
    // mTabHost.newTabSpec(tab的唯一标识).setIndicator(显示的Indicator View);
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(tag).setIndicator(getTab(tagTvid, tagImageId));
    //mTabHost.addTab(TabSpec tabSpec, Class<?>clz,Bundle args);
mTabHost.addTab(tabSpec, clz, getIntent().getExtras()); }
private View getTab(int tagTvId, int tagImageId) {    View view = getLayoutInflater().inflate(R.layout.main_activity_tab, null);    ImageView tabImg = (ImageView) view.findViewById(R.id.tab_img);    tabImg.setImageResource(tagImageId);    TextView tabTv = (TextView) view.findViewById(R.id.tab_tv);    tabTv.setText(tagTvId);
return View;
}


4、tabHost.addTab()

二、关于布局

***id--fragment_tabHost这个id我们可以自己定义,也可以用Android(android:id/tabhost)的,它是用来找到这个FragmentTabHost的。

***TabHost标签内部必须有一个id为android.R.id.tabsTabWidget标签,为什么呢?

在setUp()方法中会检查是否存在这个id的View,

if(没有这个id的View){

   1、自己创建一个LinearLayout(宽 高都是match_parent)添加到FragmentTabHost中去

   2、创建一个id为android.R.id.tabsTabWidget(宽--match_parent,高--wrap_content添加到刚刚创建LinearLayout中

   3、创建一个id为android.R.id.tabcontent的FrameLayout(宽 高都是match_parent)添加到刚刚创建的LinearLayout中,然而这个FrameLayout似乎并没有什么用。因为我们真正的内容是可以指定放在content_Framlayout_id这个里面哒,这个是一个Framlayout哦!

}else{

有的话,就什么都不做。

}

然后直接找到content_Framlayout_id对应的View设置为我们的container;

所以布局的话,你就这样吧:(平时都是白色背景,感觉好low 啊,今天菇凉也换了个黑色背景,瞬间感觉高大上了不少呢 ~ )

<android.support.v4.app.FragmentTabHost        xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@android:id/tabhost"        android:layout_width="match_parent"        android:layout_height="match_parent">    <FrameLayout              android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical">        <FrameLayout  //这个是组件要求的,好像并没有什么用                android:id="@android:id/tabcontent"                android:layout_width="0dp"                android:layout_height="0dp"                android:layout_weight="0"/>        <FrameLayout   //这个才是我们真正的container                android:id="@+id/real_content"                android:layout_width="match_parent"                android:layout_height="match_parent"/>        <TabWidget   //这个很重要啊,上面已经分析过了                android:id="@android:id/tabs"                android:layout_width="match_parent"                android:layout_gravity="bottom"                android:layout_height="@dimen/tab_widget_height"                android:background="@color/main_activity_tab_color"/>    </FrameLayout></android.support.v4.app.FragmentTabHost>

0 0
原创粉丝点击