TabHost的使用方法(不继承TabActivity)
2014-04-10 09:40
411 查看
有时候经常在一个界面中要包含TabHost(选项卡),可以用<include>标签套入一个TabHost的界面
那么TabHost的界面就根据要求,填充一些交互界面,例如:
然后在Activity中获取TabHost组件,添加每个选项卡的图片或者名称
此方法Activity不用继承TabActivity
如果想将选项卡放在底部,可以将上边的TabHost中的FrameLayout和TabWidget交换一下位置,
然后将FrameLayout的weight设置成大于1的数,其他的不便即可,参照如下:
选项卡中包含的内容就是在<include>标签下包含的内容,自己相加什么就加什么吧,哈哈
那么TabHost的界面就根据要求,填充一些交互界面,例如:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tab_test" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget //id必须是@android:id/tabs android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout //id必须是@android:id/tabcontent android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="wrap_content" > //在FrameLayout添加要填充的每个界面,就是选项卡中每个选项卡包含的内容 //这里是表示有两个选项卡,每个选项卡中的界面包含的还是布局文件 <include android:id="@+id/item1" layout="@layout/item1_layout" /> <include android:id="@+id/item2" layout="@layout/item2_layout" /> </FrameLayout> </LinearLayout> </TabHost>写到这里TabHost的布局文件就算是写完了
然后在Activity中获取TabHost组件,添加每个选项卡的图片或者名称
//获取TabHost对象 TabHost tabHost = (TabHost) findViewById(R.id.tab_test); //开始设置tabHost tabHost.setup(); //新建一个newTabSpec,设置标签(选项卡名称)和图标(setIndicator),设置内容(setContent) tabHost.addTab(tabHost.newTabSpec("Test one").setIndicator("我是第一个选项卡",getResources().getDrawable(android.R.drawable.ic_menu_call)).setContent(R.id.item1)); tabHost.addTab(tabHost.newTabSpec("Test two").setIndicator("我是第二个选项卡",getResources().getDrawable(android.R.drawable.ic_menu_camera)).setContent(R.id.item2)); //设置TabHost的背景颜色 //tabHost.setBackgroundColor(Color.argb(150,22,70,150)); //设置TabHost的背景图片资源 //tabHost.setBackgroundResource(R.drawable.bg);
此方法Activity不用继承TabActivity
如果想将选项卡放在底部,可以将上边的TabHost中的FrameLayout和TabWidget交换一下位置,
然后将FrameLayout的weight设置成大于1的数,其他的不便即可,参照如下:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tab_test" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout //id必须是@android:id/tabcontent android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" > //在FrameLayout添加要填充的每个界面,就是选项卡中每个选项卡包含的内容 //这里是表示有两个选项卡,每个选项卡中的界面包含的还是布局文件 <include android:id="@+id/item1" layout="@layout/item1_layout" /> <include android:id="@+id/item2" layout="@layout/item2_layout" /> </FrameLayout> <TabWidget //id必须是@android:id/tabs android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </TabHost>
选项卡中包含的内容就是在<include>标签下包含的内容,自己相加什么就加什么吧,哈哈
相关文章推荐
- 安卓控件使用系列29:TabHost卡片的使用方法2不继承TabActivity、TabHost控件、一个布局文件
- 安卓控件使用系列30:TabHost卡片的使用方法3不继承TabActivity、TabHost控件、多个布局文件
- tab使用 TabActivity TabHost Tabspec常用方法
- 继承TabActivity后不执行onBackPressed()里的方法
- TabWidget/TabHost的两种使用方法
- TabHost 两种使用方法 直接让一个Activity 继承TabActivity 和 利用findViwById()方法取得TagHost组件
- TabWidget/TabHost的两种使用方法
- TabWidget/TabHost的两种使用方法
- TabWidget/TabHost的两种使用方法
- tabhost的继承与不继承tabactivity
- Android TabWidget/TabHost有两种使用方法
- TabHost 两种使用方法 直接让一个Activity 继承TabActivity 和 利用findViwById()方法取得TagHost组件
- Android应用中使用TabHost组件继承TabActivity的布局方法
- Android TabActivity使用方法
- Android--学习记录--TabHost(非继承TabActivity)
- 通过实现继承TabActivity并且实现TabContentFactory的方法创建选项卡的一点心得
- Android TabActivity使用方法
- TabActivity & TabHost 的使用
- Android TahHost的使用2 不继承TabActivity
- 总结Android中TabActivity 的使用方法