TabHost是選項卡的容器,是一種特殊的FrameLayout佈局(幀佈局)。
xml佈局如下:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- 頁面內容 -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1">
</FrameLayout>
<!-- 選項卡 -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
</TabWidget>
</LinearLayout>
</TabHost>
如上代碼所示,tabhost中必須包含framelayout,tabwidget,tabhost控件的id必須爲tabhost, framelayout控件的id必須爲tabcontent,tabwidget控件的id必須爲tabs,這些是不能更改的。