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

TabActivity 與 TabHost 選項卡控件的實作教學

編輯:關於 Android 程式設計

網路上關於 Android 選項卡的教學品質參差不齊,常有漏步驟或版本混用的情況。以下整理一份可完整跑通的 TabHost 實作流程,概念清楚、可直接照抄。

整體結構

這個範例共需三個 Activity:一個作為容器的 TabActivity(MainActivity),另外兩個是各選項卡對應的內容頁(TabOne、TabTwo)。三者都必須在 manifest 中註冊。

步驟一:註冊 Activity 並設定版面

<activity
    android:name="com.example.android_tabname.MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name="com.example.android_tabname.TabOne" />
<activity android:name="com.example.android_tabname.TabTwo" />

MainActivity 的版面必須以 TabHost 為根節點,而且它的 id 一定要使用系統內建的 @android:id/tabhost,這不是風格問題而是硬性要求——TabActivity 就是靠這個 id 去找控制器的。同理,TabWidget 對應 @android:id/tabs,內容容器 FrameLayout 對應 @android:id/tabcontent。

<?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="match_parent"
    android:layout_height="match_parent">

    <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>
</TabHost>

最外層的 LinearLayout 方向必須設為 vertical,否則標籤列與內容區會疊在一起。

兩個內容頁的版面相同,這裡只列一份:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="This is TabTwo"
        android:textSize="50dip" />
</LinearLayout>

步驟二:在 Java 中組裝選項卡

流程固定為四步:取得 TabHost 實例 → 用 newTabSpec() 建立 TabSpec → 呼叫 setIndicator() 設定標籤文字與圖示、setContent() 綁定內容 → 最後 addTab() 加入容器。

package com.example.android_tabname;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class MainActivity extends TabActivity {
    private TabHost tabHost;
    private TabHost.TabSpec tabSpecA, tabSpecB;

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

        tabHost = getTabHost();

        // 第一個選項卡
        tabSpecA = tabHost.newTabSpec("One");
        tabSpecA.setIndicator("TabA",
                getResources().getDrawable(android.R.drawable.ic_media_play));
        Intent intentA = new Intent(this, TabOne.class);
        tabSpecA.setContent(intentA);

        // 第二個選項卡
        tabSpecB = tabHost.newTabSpec("Two");
        tabSpecB.setIndicator("TabB",
                getResources().getDrawable(android.R.drawable.ic_media_next));
        Intent intentB = new Intent(this, TabTwo.class);
        tabSpecB.setContent(intentB);

        // 加入 TabHost
        tabHost.addTab(tabSpecA);
        tabHost.addTab(tabSpecB);
    }
}

實務建議:選項卡數量固定時,為每個 TabSpec 配置獨立變數即可;數量動態變化時再改用集合管理,避免多個 TabSpec 共用同一個參照而互相覆寫。

步驟三:兩個內容 Activity

這兩個類別目前只負責載入版面,後續可依需求擴充各自的邏輯。

package com.example.android_tabname;

import android.app.Activity;
import android.os.Bundle;

public class TabOne extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_tabone);
    }
}
package com.example.android_tabname;

import android.app.Activity;
import android.os.Bundle;

public class TabTwo extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_tabtwo);
    }
}

後續方向

TabActivity 在 API 13 之後已被標記為老舊寫法,官方推薦改用 FragmentActivity 搭配 TabLayout 與 ViewPager。不過理解 TabHost 的「容器 + TabSpec + 內容 Intent」這層模型仍有價值,遷移到 Fragment 版本時,概念幾乎可以一比一對應:TabSpec 變成 TabLayout 的 Tab,內容 Intent 變成 Fragment。

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