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

Android UI 組件之 TabHost:多標籤頁界面的實現方式

編輯:關於 Android 程式設計

TabHost 曾經是多標籤頁界面的標準答案。雖然新項目更推薦用 Fragment 加 ViewPager,但理解 TabHost 對維護老項目仍有價值,何況它對「一個容器承載多組佈局」的思路依然適用。

TabHost 是什麼

TabHost 允許在一個窗口上放置多個標籤頁,每個標籤頁相當於獲得了一塊與外部容器等大的組件擺放區域,於是可以在一個容器裡塞進更多內容。

與它搭檔工作的還有兩個角色:TabWidget 代表選項卡的標籤條;TabSpec 代表其中一個標籤頁。

TabHost 本身只是個簡單容器,提供兩個方法來創建與添加選項卡:

  • newTabSpec(String tag):創建選項卡
  • addTab(TabHost.TabSpec tabSpec):添加選項卡

使用步驟

  1. 在佈局中定義 TabHost 組件,並為它定義各選項卡的內容
  2. Activity 繼承 TabActivity
  3. 呼叫 TabActivity.getTabHost() 取得 TabHost 物件
  4. 透過 TabHost 的方法創建並添加選項卡

TabHost 內部需要組合兩個組件:TabWidget 定義標題條,FrameLayout 負責「層疊」多個選項頁面。

一個關鍵細節:三個 ID 不能自己寫

TabHost、TabWidget、FrameLayout 這三個組件的 ID 是系統約定好的,必須引用 Android 已有的 ID:

  • TabHost:@android:id/tabhost
  • TabWidget:@android:id/tabs
  • FrameLayout:@android:id/tabcontent

寫錯或不寫,運行時會直接拋出「TabHost must have a TabWidget」之類的異常。

Java 代碼

package com.gc.tabhost;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 獲取該 Activity 裡面的 TabHost 組件
        TabHost tabHost = getTabHost();

        // 第一個 Tab 頁
        TabSpec tab1 = tabHost.newTabSpec("tab1")
                .setIndicator("Android將軍1")
                .setContent(R.id.tab01);
        tabHost.addTab(tab1);

        // 第二個 Tab 頁,帶圖標
        TabSpec tab2 = tabHost.newTabSpec("tab2")
                .setIndicator("Android將軍2", getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(R.id.tab02);
        tabHost.addTab(tab2);

        // 第三個 Tab 頁
        TabSpec tab3 = tabHost.newTabSpec("tab3")
                .setIndicator("Android將軍3")
                .setContent(R.id.tab03);
        tabHost.addTab(tab3);
    }
}

佈局文件



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <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="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="Android將軍" />
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="赳赳老秦,共赴國難,秦朝將軍" />
                </LinearLayout>

                <!-- 第二個標簽頁的內容 -->
                <LinearLayout
                    android:id="@+id/tab02"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="Android將軍2" />
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="赳赳老秦,共赴國難,秦朝將軍2" />
                </LinearLayout>

                <!-- 第三個標簽頁的內容 -->
                <LinearLayout
                    android:id="@+id/tab03"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="Android將軍3" />
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="赳赳老秦,共赴國難,秦朝將軍3" />
                </LinearLayout>

            </frameLayout>

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true" />

        </RelativeLayout>

    </LinearLayout>

</TabHost>

運行效果

為什麼現在不推薦了

TabActivity 已被標記為 Deprecated。它要求 Activity 必須走繼承體系,破壞了代碼的靈活性;而且每個標籤頁的內容全部寫在同一個佈局裡,頁面一多佈局層級會變得很深。現代做法是用 Fragment 配合 FragmentPagerAdapter 或 ViewPager2,每個標籤頁獨立管理自己的生命周期,好處是視圖可以被延遲創建與回收,切換時也更省記憶體。

轉載請注明出處:https://blog.csdn.net/android_jiangjun/article/details/25346627

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