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

Android 切換卡實作教學:TabHost 與 TabWidget 用法完整示範

編輯:關於 Android 程式設計

想讓 App 有「電話簿」那種分頁切換的視覺效果嗎?Android 內建的 TabHost 搭配 TabWidget,是最容易上手的一條路。本文透過一個可直接運行的小範例,把整個流程完整走一遍。

先認識三個關鍵角色

TabHost 是承載多個分頁的容器,而每一個分頁都可以掛上屬於自己的版面配置——電話簿裡的那些分頁,本質上就是一個個 LinearLayout。負責畫出上方那排可點擊標籤的是 TabWidget,真正用來替換內容的則是 FrameLayout。

使用順序相当固定:先用 getTabHost() 取得 TabHost 物件,再用 addTab() 把分頁一個個塞進去。每個 TabSpec 透過 setIndicator() 設定標題文字與圖示,再以 setContent() 綁定要顯示的 View。如果想在切換時收到通知,只要掛上 setOnTabChangedListener 就行。

Java 程式碼:TabTest.java

package org.hualang.tab;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;

public class TabTest extends TabActivity {
    TabHost tabhost;

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

        // 取得 TabHost 物件
        tabhost = getTabHost();

        // 新增分頁:newTabSpec 建立規格,setIndicator 設定文字與圖示,setContent 綁定內容
        tabhost.addTab(tabhost.newTabSpec("tab1")
                .setIndicator("TAB 1", getResources().getDrawable(R.drawable.img1))
                .setContent(R.id.text1));
        tabhost.addTab(tabhost.newTabSpec("tab2")
                .setIndicator("TAB 2", getResources().getDrawable(R.drawable.img2))
                .setContent(R.id.text2));
        tabhost.addTab(tabhost.newTabSpec("tab3")
                .setIndicator("TAB 3", getResources().getDrawable(R.drawable.img3))
                .setContent(R.id.text3));

        // 也可以改背景色,或改用背景圖片資源
        // tabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));
        tabhost.setBackgroundResource(R.drawable.bg0);

        // 指定啟動時顯示哪一個分頁(索引從 0 開始)
        tabhost.setCurrentTab(0);

        // 監聽分頁切換事件
        tabhost.setOnTabChangedListener(new OnTabChangeListener() {
            public void onTabChanged(String tabId) {
                Toast toast = Toast.makeText(getApplicationContext(),
                        "現在是" + tabId + "標簽", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
    }
}

版面配置:main.xml

<?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/text1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="選項卡1" />

            <TextView
                android:id="@+id/text2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="選項卡2" />

            <TextView
                android:id="@+id/text3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="選項卡3" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

最容易踩到的細節

這段配置裡有三個 id 是寫死的,必須完全照抄:@android:id/tabhost、@android:id/tabs 與 @android:id/tabcontent。任何一個打錯,TabHost 就會在執行期找不到對應的檢視而拋出例外。

另外要留意的是,TabActivity 這種寫法在後續版本中被視為舊式做法;若你在新專案中使用,建議改用 Fragment 搭配 TabLayout 來達成同樣的效果,但底層的分頁切換邏輯與這裡描述的觀念完全一致。

以上就是 TabHost 與 TabWidget 的完整實作流程,希望對你的 Android 開發有所幫助。

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