TabHost、TabWidgt、Tabcontent之间的关系
来源:互联网 发布:linux telnet ip 端口 编辑:程序博客网 时间:2024/06/02 06:01
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写法:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right|center_vertical" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" /> </LinearLayout></TabHost>
- FragmentTabHost标准的layout的写法:
<?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/maintab_toolbar_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>
对应的代码:
protected void onCreate(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到底是什么东东
也就是说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_background"
0 0
- TabHost、TabWidgt、Tabcontent之间的关系
- [BUG]Your TabHost must have a TabWidget whose id attribute is ‘android.R.id.tabcontent’
- 5 Your TabHost must have a TabWidget whose id attribute is ‘android.R.id.tabcontent’
- [ERROR]Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabcontent'
- 隐藏tabwidgt 用radiogroup来实现tab的切换
- 隐藏tabwidgt 用radiogroup来实现tab的切换
- R.id.tabcontent报错的一些问题
- android fragmenttabhost 不能将tabcontent定义在外面的解决办法
- 线程之间的关系
- 类之间的关系
- 和之间的关系
- 类之间的关系
- 类之间的关系
- 表之间的关系
- 类之间的关系
- 类之间的关系
- 类之间的关系
- 类之间的关系
- OSCACHE应用
- linux中sudo命令的使用方法
- COPRA-for-AutoCAD 2005
- HTML5解决跨域问题
- IOS添加通讯录
- TabHost、TabWidgt、Tabcontent之间的关系
- GCC: Compiling an OpenCL host on Windows
- 数组、随机数、时间、字符串在购物系统中的应用
- C语言实现全排列
- Eclipse安装ADT和介绍DDMS
- C#创建线程三种方式
- Telelogic.Rhapsody.v7.4.Windows-ISO 1CD(嵌入式仿真开发)
- 软件测试
- CoreData