一、相关概念
与TabHost结合使用的还有如下组件。
TabWidget:代表选项卡的标签条。
TabSpec:代表选项卡的一个Tab页面。
TabHost仅仅是一个简单的容器,它提供了如下两个方法来创建、添加选项卡。
newTabSpec(String tag):创建选项卡。
addTab(TabHost.TabSpec tabSpec):添加选项卡。
使用TabHost的一般步骤如下。
1、在界面布局中定义TabHost组件,并为该组件定义该选项卡的内容。
2、Activity应该继承TabActivity。
3、调用TabActivity的getTabHost()方法获取TabHost对象。
4、通过TabHost对象的方法来创建、添加选项卡。
除此之外,TabHost还提供了一些获取当前选项卡,获取当前View的方法,具体可以参考API文档。如果程序需要监控TabHost里当前标签页的改变,可以为它设置TabHost.OnTabChangeListener监听器。
二、示例
main.xml:
TabHost的ID应该为@android:id/tabhost
TabWidget的ID应该为@android:id/tabs
FrameLayout的ID应该为@android:id/tabcontent
Activity代码如下。
//TabHostTest.java
最新版本的Android平台已经不再推荐使用TabActivity,而是使用Fragment来代替TabActivity。
与TabHost结合使用的还有如下组件。
TabWidget:代表选项卡的标签条。
TabSpec:代表选项卡的一个Tab页面。
TabHost仅仅是一个简单的容器,它提供了如下两个方法来创建、添加选项卡。
newTabSpec(String tag):创建选项卡。
addTab(TabHost.TabSpec tabSpec):添加选项卡。
使用TabHost的一般步骤如下。
1、在界面布局中定义TabHost组件,并为该组件定义该选项卡的内容。
2、Activity应该继承TabActivity。
3、调用TabActivity的getTabHost()方法获取TabHost对象。
4、通过TabHost对象的方法来创建、添加选项卡。
除此之外,TabHost还提供了一些获取当前选项卡,获取当前View的方法,具体可以参考API文档。如果程序需要监控TabHost里当前标签页的改变,可以为它设置TabHost.OnTabChangeListener监听器。
二、示例
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 定义第一个标签页的内容 -->
<LinearLayout
android:id="@+id/tab01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女儿国国王 - 2012/12/12"
android:textSize="11pt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="东海龙女 - 2012/12/18"
android:textSize="11pt" />
</LinearLayout>
<!-- 定义第二个标签页的内容 -->
<LinearLayout
android:id="@+id/tab02"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="白骨精 - 2012/08/12"
android:textSize="11pt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蜘蛛精 - 2012/09/20"
android:textSize="11pt" />
</LinearLayout>
<!-- 定义第三个标签页的内容 -->
<LinearLayout
android:id="@+id/tab03"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="11pt">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="孙悟空 - 2012/09/19"
android:textSize="11pt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="猪八戒 - 2012/10/12"
android:textSize="11pt" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
注意上面的布局文件中,TabHost容器内部需要组合两个组件:TabWidget和FrameLayout,其中TabWidget定义选项卡的标签条;FramLayout则用于“层叠”组合多个选项页面。不仅如此,上面的布局文件中这三个组件的ID也有要求。
TabHost的ID应该为@android:id/tabhost
TabWidget的ID应该为@android:id/tabs
FrameLayout的ID应该为@android:id/tabcontent
Activity代码如下。
//TabHostTest.java
public class TabHostTest extends TabActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取该Activity里面的TabHost组件
TabHost tabHost = getTabHost();
// 创建第一个Tab页
TabSpec tab1 = tabHost.newTabSpec("tab1")
.setIndicator("已接电话") // 设置标题
.setContent(R.id.tab01); //设置内容
// 添加第一个标签页
tabHost.addTab(tab1);
TabSpec tab2 = tabHost.newTabSpec("tab2")
// 在标签标题上放置图标
.setIndicator("呼出电话", getResources()
.getDrawable(R.drawable.ic_launcher))
.setContent(R.id.tab02);
// 添加第二个标签页
tabHost.addTab(tab2);
TabSpec tab3 = tabHost.newTabSpec("tab3")
.setIndicator("未接电话")
.setContent(R.id.tab03);
// 添加第三个标签页
tabHost.addTab(tab3);
}
}
上面的程序调用了TabHost.TabSpec对象的setContent(int viewId)方法来设置标签页的内容;除此之外还可调用setContent(Intent intent)方法来设置标签页内容,Intent还可用于启动其他Activity——这意味着TabHost.TabSpec可直接装载另一个Activity。
最新版本的Android平台已经不再推荐使用TabActivity,而是使用Fragment来代替TabActivity。