編輯:關於 Android 程式設計
TabHost 是早期 Android 實作分頁切換的標準做法。它的結構有幾個「id 必須寫死」的硬性規定,很多人卡關其實都栽在這裡。本文先把三個必要元素的規則講清楚,再給完整範例。
TabHost 的 id 必須引用 Android 系統自帶的資源:
android:id=@android:id/tabhost ;
只有設定了這個 id,Activity 中才能用 getHost() 風格的方法(實際上是 findViewById 搭配 tabhost)取得該 TabHost 物件。範例:
<TabHost android:id="@android:id/tabhost" android:layout_height="match_parent" android:layout_width="match_parent"></TabHost>
<TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal"></TabWidget>
<FrameLayout android:id="@android:id/tabcontent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent"></FrameLayout>

專案結構:

main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/hometabs" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- TabHost必須包含一個 TabWidget和一個FrameLayout--> <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- TabWidget的id屬性必須為 @android:id/tabs--> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TabWidget> <!-- FrameLayout的id屬性必須為 @android:id/tabcontent--> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
Java 程式碼:
package cn.com.tagHost.test; import android.app.Activity; import android.os.Bundle; import android.widget.TabHost; import android.widget.TabWidget; public class TagHostTest2 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 獲取TabHost對象 TabHost tabHost = (TabHost) findViewById(R.id.tabhost); // 如果沒有繼承TabActivity時,通過該種方法加載啟動tabHost tabHost.setup(); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一個標簽", getResources().getDrawable(R.drawable.icon)).setContent( R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三個標簽") .setContent(R.id.view3)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二個標簽") .setContent(R.id.view2)); } }
這裡最容易漏掉的是 tabHost.setup()。沒繼承 TabActivity 時,必須手動呼叫它完成初始化,否則 addTab 會直接出錯。
addTab() 的串接寫法值得熟悉:newTabSpec(標籤標識) → setIndicator(文字, 圖示) → setContent(內容)。setContent 可傳入 view 的 id(對應 FrameLayout 中的子元件),也可傳 Intent。
第一,版面格式與兩個 id 屬性值必須完全照規矩寫,@android:id/tabs 與 @android:id/tabcontent 寫錯一個字就會失效。
第二,TabWidget 代表「標籤列」,FrameLayout 代表「點下去看到的內容」。FrameLayout 裡宣告的元件只是「具備成為內容的資格」,真正顯示哪一頁仍要在程式碼中透過 setContent 指定。

只改版面檔就行:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/hometabs" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- TabHost必須包含一個 TabWidget和一個FrameLayout--> <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- FrameLayout的id屬性必須為 @android:id/tabcontent--> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="hello baby!" /> <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </FrameLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- TabWidget的id屬性必須為 @android:id/tabs--> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:paddingBottom="0dp" > </TabWidget> </RelativeLayout> </LinearLayout> </TabHost> </LinearLayout>
兩個變化:一是順序對調,FrameLayout 在上、TabWidget 在下;二是把 TabWidget 包進 RelativeLayout,用 android:layout_alignParentBottom="true" 讓它貼齊底部——這個屬性只在 RelativeLayout 中有效,也正是要多包一層的原因。順帶一提,paddingBottom 設為 0dp 是為了避免系統預設內距讓標籤列離底邊還有一條縫。
坦白說,TabHost 與 TabActivity 在 API 13 之後都已標記為 deprecated。新專案請改用 TabLayout 搭配 ViewPager2 與 Fragment,或直接在頁面內用 Fragment 切換。好處是版面完全自訂、不必被那兩個寫死的 id 綁住,也能拿到滑動切頁與指示器動畫。本文的規則仍有理解價值,尤其在維護老專案時。
深入解析 Android 如何檢查與設定預設應用程式
作為一套功能完整的行動作業系統,Android 早已內建了「預設開啟程式」的機制。當同一種動作有多個 App 都能勝任時,系統便能記住使用者的偏好,下次直接叫出指定的應用
Android 增量升級原理與實踐(附 bsdiff/bspatch 與 JNI 集成代碼)
硬件在不斷升級,分辨率越來越高,安裝包自然也越來越大。功能機時代一個 App 上 1MB 就算大的,2MB 已經很誇張;如今哪怕只改了一個標題,也不該逼用戶重新下載幾
手把手做藍牙小車:Arduino 入門實作
選擇 Arduino 開發板 對軟體開發者而言,Arduino 是認識硬體的友善入口。它把複雜的電路細節封裝起來,讓開發者能用熟悉的程式概念控制腳位、讀取資料。 官方版與
Android 動畫:讓齒輪永不停歇地轉動
很多 loading 或裝飾性動畫裡都會用到「轉個不停的齒輪」效果。實現起來其實很簡單,用 RotateAnimation 就能讓齒輪無限勻速旋轉。 (這裡是靜態圖,實