用Eclipse可視化佈局生成的以下句子
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<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" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
然後Activity裏面的代碼
TabHost mTabHost = (TabHost) mView.findViewById(android.R.id.tabhost);
mTabHost.setup();
TabSpec ts1 = mTabHost.newTabSpec("tab1");
ts1.setContent(R.id.tab1);
ts1.setIndicator("互動");// 設置tab名字,可以自定義view
TabSpec ts2 = mTabHost.newTabSpec("tab2");
ts2.setContent(R.id.tab2);
ts2.setIndicator("互動");
mTabHost.addTab(ts1);
mTabHost.addTab(ts2);
.setup()。
Call setup() before adding tabs if loading TabHost using findViewById(). However: You do not need to call setup() after getTabHost() in
TabActivity
. Example:
如果使用findViewById()加載TabHost,在添加tab之前調用setup(),如果是繼承TabActivity就可以不用.setup()了