Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

詳解Android TabHost組件的布局要點

編輯:關於 Android 程式設計

TabHost 是早期 Android 實作分頁切換的標準做法。它的結構有幾個「id 必須寫死」的硬性規定,很多人卡關其實都栽在這裡。本文先把三個必要元素的規則講清楚,再給完整範例。

1. 根標籤與 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>

2. TabWidget 組件

  • 作用:負責選項卡切換按鈕,點擊即可換頁。
  • id 必須是 android:id="@android:id/tabs"。
  • 必要元素:它與 FrameLayout 是 TabHost 中必備的兩個子元件。
  • 放到下方:若想讓切換鈕顯示在底部,把 TabWidget 宣告在 FrameLayout 之後即可,但此時 FrameLayout 要設 android:layout_weight="1" 撐開空間。
  • 調整大小:可透過與 FrameLayout 的權重比例來控制按鈕區高度。

<TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal"></TabWidget>

3. FrameLayout 組件

  • 作用:其中定義的子元件就是各頁要顯示的內容,TabHost 的頁面視圖都放在這裡。
  • id 必須是 android:id="@android:id/tabcontent"。

<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

坦白說,TabHost 與 TabActivity 在 API 13 之後都已標記為 deprecated。新專案請改用 TabLayout 搭配 ViewPager2 與 Fragment,或直接在頁面內用 Fragment 切換。好處是版面完全自訂、不必被那兩個寫死的 id 綁住,也能拿到滑動切頁與指示器動畫。本文的規則仍有理解價值,尤其在維護老專案時。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved