Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 開發入門

Android 分頁元件 TabWidget 實作:版面配置與切換事件

編輯:開發入門

TabHost 是 Android 早期用來做分頁切換的元件,結構上有三個部分缺一不可:一個 TabHost 作為容器、一個 TabWidget 負責顯示頁籤列、一個 FrameLayout 負責容納各頁的內容。三者都要用系統預先定義好的 id,這點很容易寫錯。

版面檔

<?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/textview1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is a tab" />

            <TextView
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is another tab" />

            <TextView
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is a third tab" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

這裡最關鍵的一點:tabhost、tabs、tabcontent 三個 id 都必須寫成 @android:id/ 開頭,不能自訂。TabHost 內部是靠這三個固定 id 去找對應元件的,寫錯就會在執行期拿到 null。另外,各頁的內容元件都放在同一個 FrameLayout 裡,透過各自的 id 被 TabHost 逐一對應到頁籤上。

Activity 端

package com.yarin.android.TestOnWidget;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class mytestWidget extends TabActivity {

    // 宣告 TabHost 物件
    TabHost mTabHost;

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

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

        // 新增頁籤的三個步驟:
        // 1. newTabSpec() 建立規格,參數是這個頁籤的 tag
        // 2. setIndicator() 設定標題文字與圖示
        // 3. setContent() 指定這一頁要顯示哪個 View
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
                .setIndicator("TAB 1", getResources().getDrawable(R.drawable.img1))
                .setContent(R.id.textview1));

        mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
                .setIndicator("TAB 2", getResources().getDrawable(R.drawable.img2))
                .setContent(R.id.textview2));

        mTabHost.addTab(mTabHost.newTabSpec("tab_test3")
                .setIndicator("TAB 3", getResources().getDrawable(R.drawable.img3))
                .setContent(R.id.textview3));

        // 設定背景顏色
        mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));

        // 也可以改用背景圖片
        // mTabHost.setBackgroundResource(R.drawable.bg0);

        // 指定一開始顯示哪一頁(索引從 0 起算)
        mTabHost.setCurrentTab(0);

        // 頁籤切換事件
        mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {
                Dialog dialog = new AlertDialog.Builder(mytestWidget.this)
                        .setTitle("提示")
                        .setMessage("當前選中:" + tabId + " 標籤")
                        .setPositiveButton("確定",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        dialog.cancel();
                                    }
                                })
                        .create();
                dialog.show();
            }
        });
    }
}

幾個實作重點

  • Activity 必須繼承 TabActivity。getTabHost() 是 TabActivity 提供的方法,用一般 Activity 拿不到。
  • setIndicator() 有兩種形式。只傳字串的版本會顯示純文字頁籤;傳字串加 Drawable 的版本則是圖文並用。圖示尺寸最好事先統一,否則頁籤寬度會參差。
  • setContent() 除了傳 view id,也可以傳 Intent。如果你希望每一頁背後是一個獨立的 Activity(擁有自己的生命週期),用 Intent 版本會更合適;只是單純切換畫面上的 View 時,用 id 就夠了。
  • onTabChanged() 帶的參數是 tag,不是索引。也就是你在 newTabSpec() 裡給的字串,別誤以為它是位置數字。
  • 切換監聽器容易觸發過多次。如果你在 onTabChanged() 裡又呼叫 setCurrentTab(),會形成迴圈。這類操作建議加一個旗標防護。

最後補充一句:TabHost 與 TabActivity 在 Android 3.0 之後已被標示為 deprecated,新專案請改用 Fragment 搭配 TabLayout 或 ViewPager。不過理解 TabHost 的「容器 + 頁籤列 + 內容區」三段式結構,對之後學 Fragment 分頁仍很有幫助。

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