<?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="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!--省略部分代码--> <TextView android:id="@+id/no_team_meetings" android:textSize="18sp" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <TextView android:id="@+id/no_team_meeting_stats" android:textSize="18sp" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </FrameLayout> </LinearLayout> </TabHost>
通常我们采用线性布局,所以<TabHost> 的子元素是 <LinearLayout>。<TabWidget>对应Tab。<FrameLayout>则用于包含Tab需要展示的内容。需要注意的是<TabWidger> 和<FrameLayout>的Id 必须使用系统id,分别为android:id/tabs 和 android:id/tabcontent 。因为系统会使用者两个id来初始化TabHost的两个实例变量(mTabWidget 和 mTabContent)。
private void createTabs() { TabHost tabhost=getTabHost(); tabhost.addTab(tabhost.newTabSpec("stats_tab"). setIndicator(CharSequence label). setContent( int viewId); tabhost.addTab(tabhost.newTabSpec("meetings_tab"). setIndicator(CharSequence label). setContent( int viewId); getTabHost().setCurrentTab(0); }
mTabHost = (TabHost)findViewById(R.id.tabhost); mTabHost.setup(); mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");