TabHost使用總結

一、相關概念
與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。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章