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

Android TabWidget 類別 API 詳解: XML 屬性與常用方法一次整理

編輯:Android 開發實例

本文整理 android.widget.TabWidget 的 API 說明,並搭配屬性表、方法清單與可運行的範例程式碼,方便查閱與對照實作。

一、類別結構

public class TabWidget extends LinearLayout
        implements View.OnFocusChangeListener

java.lang.Object
  └─ android.view.View
       └─ android.view.ViewGroup
            └─ android.widget.LinearLayout
                 └─ android.widget.TabWidget

二、功能概述

TabWidget 負責顯示一列選項卡標籤,每個標籤對應父容器 TabHost 中的一頁。當使用者點選某個標籤時,TabWidget 會通知父容器 TabHost 切換到該頁顯示。

實務上要特別注意分工:多數操作並不會直接呼叫在 TabWidget 上。新增標籤、管理回呼這些工作,都應該透過容器 TabHost 來完成。你通常只會用 TabWidget 做兩件事——走訪現有的標籤清單,或微調標籤列的視覺呈現。

三、XML 屬性

屬性名稱說明
android:divider指定在標籤之間繪製的分隔物件。
android:tabStripEnabled決定是否繪製標籤底部的分隔線。
android:tabStripLeft用於繪製標籤下方分隔線左半段的物件。
android:tabStripRight用於繪製標籤下方分隔線右半段的物件。

四、公開方法

addView(View child)

新增子檢視。若子檢視未設定版面參數,系統會套用此 ViewGroup 的預設參數。

childDrawableStateChanged(View child)

當 addStatesFromChildren() 回傳 true 時,重新整理此群組(含子元素)的視覺狀態。

dispatchDraw(Canvas canvas)

繪製流程中負責畫出子檢視。衍生類別若想在自身內容繪製前取得控制權,可覆寫此方法。

focusCurrentTab(int index)

將焦點移到指定索引的標籤並使其被選取。與 setCurrentTab() 的差別在於:後者只切換選取狀態、不搶焦點;前者則兩者都做。透過 UI 操作時,焦點會由 TabWidget 的 onFocusChanged() 自動處理;但如果你是用程式碼控制切頁,就得自己確保焦點狀態一致。

getChildTabViewAt(int index)

取得指定索引位置上的標籤檢視,索引從 0 開始。

getTabCount()

回傳目前標籤的數量。

isStripEnabled()

回報標籤底部的分隔線目前是否處於繪製狀態。

onFocusChange(View v, boolean hasFocus)

檢視焦點狀態改變時的回呼。

setCurrentTab(int index)

設定目前的選項標籤,並通知其他 UI 元素切換。這裡的「選取」是視圖邏輯層面的概念,與「焦點」不同:例如在分頁內的清單中上下捲動,UI 焦點會移動,但並不會改變分頁的選取狀態——只有切換分頁時,選取狀態才會更新。若需要同時完成選取與取焦,請改用 focusCurrentTab()。

setDividerDrawable(Drawable) / setDividerDrawable(int resId)

設定標籤之間的分隔物件,可傳入 Drawable 或資源 ID。

setEnabled(boolean enabled)

設定檢視的啟用狀態,實際意義依子類別而異。

setLeftStripDrawable / setRightStripDrawable

分別設定標籤下方分隔線的左半段與右半段外觀,同樣各有 Drawable 與資源 ID 兩種多載。

setStripEnabled(boolean stripEnabled)

控制標籤底部分隔線是否繪製,預設為繪製。若你為標籤指定了自訂檢視,TabHost 會呼叫此方法關閉底線繪製。

五、受保護方法

getChildDrawingOrder(int childCount, int i):回傳本次迭代中應繪製的子元素索引,可覆寫以調整繪製順序,預設直接回傳 i。要讓此方法生效,必須先呼叫 setChildrenDrawingOrderEnabled(true)。

onSizeChanged(int w, int h, int oldw, int oldh):檢視尺寸變更時的回呼,四個參數分別為目前寬高與先前的寬高。若檢視是動態加入階層的,先前值可視為 0。

六、名詞對照

  • indicator:指示符(標籤外觀)
  • drawable:可繪製物件
  • strips:分隔線

七、範例程式碼

以下範例展示三個分頁,並示範如何切換 TabWidget 的顯示狀態與分隔線狀態。

public class TabDemo1 extends Activity implements OnClickListener {

    TabHost mTabHost = null;
    TabWidget mTabWidget = null;
    Button btnVisiable, btnStrip;

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

        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();
        mTabWidget = mTabHost.getTabWidget();

        mTabHost.addTab(mTabHost.newTabSpec("tab1")
                .setContent(R.id.LinearLayout001).setIndicator("Tab1"));
        mTabHost.addTab(mTabHost.newTabSpec("tab2")
                .setContent(R.id.LinearLayout002).setIndicator("Tab2"));
        mTabHost.addTab(mTabHost.newTabSpec("tab3")
                .setContent(R.id.LinearLayout003).setIndicator("Tab3"));

        btnVisiable = (Button) findViewById(R.id.btnVisiable);
        btnStrip = (Button) findViewById(R.id.btnStrip);
        btnVisiable.setOnClickListener(this);
        btnStrip.setOnClickListener(this);

        setTitle("共有 " + mTabWidget.getTabCount() + " 個 tab");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnVisiable:
                if (mTabWidget.getVisibility() != android.view.View.VISIBLE) {
                    mTabWidget.setVisibility(android.view.View.VISIBLE);
                } else {
                    mTabWidget.setVisibility(android.view.View.INVISIBLE);
                }
                break;

            case R.id.btnStrip:
                mTabWidget.setStripEnabled(!mTabWidget.isStripEnabled());
                mTabWidget.setRightStripDrawable(android.R.color.transparent);
                break;

            default:
                break;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabhost">

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

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

        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">

            <LinearLayout android:id="@+id/LinearLayout001"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <Button android:id="@+id/btnVisiable"
                    android:text="顯示狀態切換"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"></Button>
                <Button android:id="@+id/btnStrip"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="分隔線狀態切換"></Button>
                <TextView android:id="@+id/TextView1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="第一個分頁" />
            </LinearLayout>

            <LinearLayout android:id="@+id/LinearLayout002"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <TextView android:id="@+id/TextView2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="第二個分頁" />
            </LinearLayout>

            <LinearLayout android:id="@+id/LinearLayout003"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <TextView android:id="@+id/TextView3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="第三個分頁" />
            </LinearLayout>

        </FrameLayout>
    </LinearLayout>
</TabHost>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved