TabHost、TabWidgt、Tabcontent之间的关系

TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。

TabWidget就是每个tab的标签,FrameLayout则是tab内容。

TabHost、TabWidget、FrameLayout的anroid:id不能随意设置,必须指定id为:

android:id="@android:id/tabhost"

android:id="@android:id/tabs"

android:id="@android:id/tabcontent"

老版本标准的tabhost layout写法:

[html]view plaincopy

android:id="@android:id/tabhost"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="5dp">

android:id="@android:id/tabs"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="right|center_vertical"/>

android:id="@android:id/tabcontent"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="5dp"/>

FragmentTabHost标准的layout的写法:

[html]view plaincopy


android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

android:id="@+id/realtabcontent"

android:layout_width="fill_parent"

android:layout_height="0dip"

android:layout_weight="1"/>

android:id="@android:id/tabhost"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@drawable/maintab_toolbar_bg">

android:id="@android:id/tabcontent"

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_weight="0"/>

对应的代码:

[java]view plaincopy

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.fragment_tabs);

mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);

mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),

FragmentStackSupport.CountingFragment.class,null);

mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),

LoaderCursorSupport.CursorLoaderListFragment.class,null);

mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),

LoaderCustomSupport.AppListFragment.class,null);

mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),

LoaderThrottleSupport.ThrottledLoaderListFragment.class,null);

}

为什么FragmentTabHost的layout要这样定

答:没有为什么,Android官方文档就是这样用的!:

http://developer.android.com/reference/android/app/TabActivity.html

和老版本的TabHost的有何异同?

可以看出,FragmentTabHost中,默认不需要TabWidget,不过你硬要多此一举,添加进来也没问题,

其实FragmentTabHost继承自TabHost

那么试着将老版本的tab_right_gravity.xml中的TabWidget,和TabContent定义去掉呢?

答:TabHost.setup()时,找不到android:id="@android:id/tabs",直接crash。

最后,你要理解Indicator到底是什么东东

Indicator其实就是TabWidget的子view,从继承关系上可以看出,TabWidget继承自ViewGroup。

也就是说TabWidget有多个子view,那么每个子view就是一个Indicator,

就是我们用手指去点的那些个tab,也可以说是标签。

TabSpec.setIndicator(View),这里的view可以是你自定义的tab indicator的view,

之后你可以通过TabHost.getTabWidget().getChildTabViewAt(i)获取到指定位置的indicator view,

比如下面这句就是获取到指定位置的indicator view,为其设置背景:

mTabHost.getTabWidget().getChildTabViewAt(i).setBackgroundResource(R.drawable.selector_tab_background);

实际上如果你真的要设置indicator的每个子view的背景的话,这样干就把简单的事搞复杂了,

为什么不在indicator view的layout里面直接指定background呢:android:background="@drawable/selector_tab

更多相关文章

  1. Ubuntu和Win下:Android(安卓)Studio and Gradle are using differ
  2. TextView 实现原理 实践
  3. Android隐藏标题栏的方法
  4. Android(安卓)详解build.gradle文件
  5. 【Android每日点滴】Fragment与Activity交互
  6. 初学Android,音频管理器之控制音频(六十六)
  7. Cordova 插件 plugin.xml 文件配置
  8. Android(安卓)- 原生的动作意图(native activity intent)
  9. Android(安卓)studio Xposed框架Hook小试

随机推荐

  1. Android 6.0 (API 23, M)以上对permissio
  2. android安全问题(七) 抢先接收广播 - 内因
  3. Android对话框的高级设置《一》设置对话
  4. Android(安卓)WebView 上传文件支持全解
  5. Android默认系统声音/大小修改及配置
  6. Android 使用基于位置的服务(二)
  7. 内存优化之其他优化(容器数据遍历方案,arra
  8. Android(安卓)m6.0权限问题调用封装utils
  9. android的apk文件的xml提取
  10. Android底部tab与标题栏相结合