它可以把所有的標籤在不同的活動。創建簡單的佈局爲你的父母的活動,這樣的事情:
<?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"
android:padding="5dp">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
現在你已經創造了你想要的標籤佈局,現在是時候你想要的活動,以填補你的標籤。這是在TabActivity的onCreate方法中完成的。
public class MyTabActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, YourFirstActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("NameOfSpec").setIndicator("TabText1"),null).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, YourSecondActivity.class);
spec = tabHost.newTabSpec("NameOfSpec2").setIndicator("TabText2",null).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
這就是它,現在你有一個帶有2個選項卡的tabhost,添加更多隻是添加更多的tabhosts。當然,您需要更改每個選項卡的文本,並填寫自己的活動,而不是「YourFirstActivity」和「YourSecondActivity」。
對於一個教程,看看這裏:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html