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

Android TabWidget 標籤切換頁的實作應用

編輯:關於 Android 程式設計

TabWidget 能實現類似 Android 聯絡人那樣的多標籤切換效果,讓使用者透過點擊不同標籤顯示不同內容。要實現它,首先要認識 TabHost——一個用來容納多個 Tab 標籤的容器。每個 Tab 都能對應自己的佈局。

核心概念

使用 TabHost 時,先透過 getTabHost() 取得物件,再用 addTab() 逐一加入 Tab。每個 Tab 在切換時都會產生事件,若要捕捉,可設定 setOnTabChangedListener 監聽器。

步驟一:佈局檔

TabHost 內部需包含一個 TabWidget(顯示標籤頁)與一個 FrameLayout(顯示內容),並使用系統提供的專用 id:

<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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <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="Linux"
                android:textColor="#FF0000" />
            <TextView
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="MAC"
                android:textColor="#385E0F" />
            <TextView
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Windows"
                android:textColor="#1E90FF" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

步驟二:修改 MainActivity(繼承 TabActivity)

在 addTab() 中,透過 newTabSpec 建立每個標籤,以 setIndicator 設定標籤文字與圖示、setContent 指定對應的內容 View:

public class MainActivity extends TabActivity {
    private TabHost tabHost;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabHost = getTabHost();
        addTab(); // 添加標籤
        tabHost.setBackgroundColor(Color.argb(150, 20, 80, 150)); // 設定背景顏色
        tabHost.setBackgroundResource(R.drawable.ic_launcher);    // 設定背景圖片
        tabHost.setCurrentTab(0); // 設定預設顯示的標籤(索引從 0 開始)
        // 標籤切換事件:從一個標籤切換到另一個時觸發
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("提示");
                builder.setMessage("當前選中了" + tabId + "標籤");
                builder.setPositiveButton("確定", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.create().show();
            }
        });
    }
    // 為 TabHost 添加標籤
    private void addTab() {
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("TAB1", getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(R.id.textview1));
        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("TAB2", getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(R.id.textview2));
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("TAB3", getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(R.id.textview3));
    }
}

步驟三:運行成果

要特別留意的是,setOnTabChangedListener 監聽的是「標籤切換」事件(從一個標籤切到另一個),而非單純的點擊事件。掌握 TabHost、TabWidget 與 TabSpec 的配合,你就能輕鬆打造多頁籤的切換介面。

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