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

Android 入門:用 TabHost 與 TabWidget 做分頁標籤

編輯:Android 開發實例

Tab 是行動介面裡最直覺的分頁方式,能把大量內容收進同一個螢幕,讓使用者左右切換而不必層層進入。代價是開發複雜度上升,所以建議真的有必要時再採用。

Android 的 Tab 實作有個讓新手困惑的地方:XML 結構必須嚴格遵守三層巢狀與特定 id 命名,少一層或名字寫錯就會在執行期爆炸。

結構鐵律

順序與命名都不能改:

TabHost → TabWidget(id 必須是 @android:id/tabs)→ FrameLayout(id 必須是 @android:id/tabcontent)。這兩個 id 是寫死在框架裡的,自訂名稱不會被辨識。

先看一下執行結果:

版面檔 main.xml

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

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

    <FrameLayout android:id="@android:id/tabcontent"
        android:paddingTop="65px"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout android:layout_height="wrap_content"
            android:id="@+id/Tab1"
            android:orientation="vertical"
            android:layout_width="fill_parent">
            <EditText android:layout_height="wrap_content"
                android:id="@+id/edtTab1"
                android:layout_width="fill_parent"></EditText>
            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnTab1" android:text="Tab1"></Button>
        </LinearLayout>

        <LinearLayout android:layout_height="wrap_content"
            android:id="@+id/Tab2"
            android:layout_width="fill_parent"
            android:orientation="horizontal">
            <EditText android:layout_height="wrap_content"
                android:id="@+id/edtTab2"
                android:layout_width="wrap_content"
                android:layout_weight="300"></EditText>
            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnTab2" android:text="Tab2"></Button>
        </LinearLayout>

    </FrameLayout>
</TabHost>

注意 FrameLayout 裡每個子 LinearLayout 都有自己的 id,這個 id 稍後會被當作 Tab 的內容指向。

Java 程式碼

Activity 要繼承 TabActivity,透過 getTabHost() 取得容器,再用 newTabSpec() 一個一個把分頁掛上去。

package com.testTab;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class testTab extends TabActivity {
    Button btnTab1, btnTab2;
    EditText edtTab1, edtTab2;

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

        TabHost tabs = getTabHost();

        // 設定 Tab1
        TabSpec tab1 = tabs.newTabSpec("tab1");
        tab1.setIndicator("tab1");        // 分頁標題文字
        tab1.setContent(R.id.Tab1);       // 關聯的容器 id
        tabs.addTab(tab1);

        btnTab1 = (Button) this.findViewById(R.id.btnTab1);
        edtTab1 = (EditText) this.findViewById(R.id.edtTab1);
        btnTab1.setOnClickListener(new ClickEvent());

        // 設定 Tab2
        TabSpec tab2 = tabs.newTabSpec("tab2");
        tab2.setIndicator("tab2");
        tab2.setContent(R.id.Tab2);
        tabs.addTab(tab2);

        btnTab2 = (Button) this.findViewById(R.id.btnTab2);
        edtTab2 = (EditText) this.findViewById(R.id.edtTab2);
        btnTab2.setOnClickListener(new ClickEvent());

        tabs.setCurrentTab(0);            // 預設停在第一頁
    }

    class ClickEvent implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (v == btnTab1) {
                edtTab1.setText("tab1");
            } else if (v == btnTab2) {
                edtTab2.setText("tab2");
            }
        }
    }
}

幾個實務提醒

setIndicator() 除了傳字串,也可以傳 (icon, title) 的重載版本來顯示圖示。

若想用自訂圖當分頁標籤並做出選中/未選中的視覺差異,需要準備 selector 的 state_list drawable,透過 tab1.setIndicator(R.drawable.tab_icon) 指定。

最後要說的是:TabActivity 與 TabHost 都已被標記為 deprecated。新專案建議改用 TabLayout 搭配 ViewPager2 與 Fragment,API 更直覺、行為也更符合 Material Design 規範。上面這套寫法主要價值在於理解 Tab 背後「容器切換」的思維,以及維護老專案時用得上。

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