編輯:開發入門
TabHost 是 Android 早期用來做分頁切換的元件,結構上有三個部分缺一不可:一個 TabHost 作為容器、一個 TabWidget 負責顯示頁籤列、一個 FrameLayout 負責容納各頁的內容。三者都要用系統預先定義好的 id,這點很容易寫錯。
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="https://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/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a tab" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
這裡最關鍵的一點:tabhost、tabs、tabcontent 三個 id 都必須寫成 @android:id/ 開頭,不能自訂。TabHost 內部是靠這三個固定 id 去找對應元件的,寫錯就會在執行期拿到 null。另外,各頁的內容元件都放在同一個 FrameLayout 裡,透過各自的 id 被 TabHost 逐一對應到頁籤上。
package com.yarin.android.TestOnWidget;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class mytestWidget extends TabActivity {
// 宣告 TabHost 物件
TabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得 TabHost 物件
mTabHost = getTabHost();
// 新增頁籤的三個步驟:
// 1. newTabSpec() 建立規格,參數是這個頁籤的 tag
// 2. setIndicator() 設定標題文字與圖示
// 3. setContent() 指定這一頁要顯示哪個 View
mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
.setIndicator("TAB 1", getResources().getDrawable(R.drawable.img1))
.setContent(R.id.textview1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
.setIndicator("TAB 2", getResources().getDrawable(R.drawable.img2))
.setContent(R.id.textview2));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3")
.setIndicator("TAB 3", getResources().getDrawable(R.drawable.img3))
.setContent(R.id.textview3));
// 設定背景顏色
mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
// 也可以改用背景圖片
// mTabHost.setBackgroundResource(R.drawable.bg0);
// 指定一開始顯示哪一頁(索引從 0 起算)
mTabHost.setCurrentTab(0);
// 頁籤切換事件
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
Dialog dialog = new AlertDialog.Builder(mytestWidget.this)
.setTitle("提示")
.setMessage("當前選中:" + tabId + " 標籤")
.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
})
.create();
dialog.show();
}
});
}
}
getTabHost() 是 TabActivity 提供的方法,用一般 Activity 拿不到。setIndicator() 有兩種形式。只傳字串的版本會顯示純文字頁籤;傳字串加 Drawable 的版本則是圖文並用。圖示尺寸最好事先統一,否則頁籤寬度會參差。setContent() 除了傳 view id,也可以傳 Intent。如果你希望每一頁背後是一個獨立的 Activity(擁有自己的生命週期),用 Intent 版本會更合適;只是單純切換畫面上的 View 時,用 id 就夠了。onTabChanged() 帶的參數是 tag,不是索引。也就是你在 newTabSpec() 裡給的字串,別誤以為它是位置數字。onTabChanged() 裡又呼叫 setCurrentTab(),會形成迴圈。這類操作建議加一個旗標防護。最後補充一句:TabHost 與 TabActivity 在 Android 3.0 之後已被標示為 deprecated,新專案請改用 Fragment 搭配 TabLayout 或 ViewPager。不過理解 TabHost 的「容器 + 頁籤列 + 內容區」三段式結構,對之後學 Fragment 分頁仍很有幫助。
用 Eclipse 開發 Android 應用程式(四):內容提供者與 Google Maps 實戰
打造內容提供者與 Google Maps 應用程式 你已經看過一個完整的應用程式範例,現在簡單談談更複雜的一款應用。 內容提供者與 Google Maps 本教學討論的第
智慧交通之路(一):用 Google Maps 與 GTFS 打造你的通勤後盾
公共交通能紓解擁擠、削減由此而生的污染,順帶降低通勤者的壓力與花費。今天有為數眾多的網站與行動應用,正幫著世界各地的乘客、逛街的人和旅人變成「有道德的利己主義者」——把各
動手打造 Android RSS 閱讀器(三):把資料呈現在螢幕上
在 Android 中呈現 RSS 資料 此時,RSS 訂閱源裡的 XML 資料已安全地保存在記憶體中的 RSSFeed 實例裡,這個實例用一個便利的 List 結構裝著
智慧交通之路(二):開放資料社群與實用的通勤 App
City-Go-Round:開放交通資料的集散地 City-Go-Round 這個網站於 2009 年 12 月上線。它一方面在倡議開放交通資料——按它自己的統計,美國有