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

Android TabHost 使用詳解:打造多選項卡介面

編輯:關於 Android 開發

TabHost 是 Android 自帶的選項卡(Tab)容器控件,能夠讓使用者在多個頁面之間靈活切換。下圖展示了其基本效果:

主布局文件

TabHost 內部通常需要搭配 TabWidget(負責顯示標籤)與 FrameLayout(負責承載各標籤對應的內容)。id 需分別指定為 @android:id/tabs 與 @android:id/tabcontent。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TabHost android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

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

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

            <FrameLayout android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <LinearLayout android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>

                <LinearLayout android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                </LinearLayout>

                <LinearLayout android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>

            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

核心代碼

Activity 需繼承自 ActivityGroup,透過 getLocalActivityManager() 完成 TabHost 的初始化,並使用 newTabSpec 逐一添加標籤。setContent 中傳入 Intent 是讓每個標籤關聯獨立 Activity 的關鍵。

package com.tabhost;

import android.os.Bundle;
import android.app.ActivityGroup;
import android.content.Intent;
import android.view.Window;
import android.widget.TabHost;

public class MainActivity extends ActivityGroup {
    private TabHost mTabHost;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        // 設置 TabHost
        initTabs();
    }

    private void initTabs() {
        mTabHost = (TabHost) findViewById(R.id.tabhost);
        mTabHost.setup(this.getLocalActivityManager());

        // 添加日誌列表的 tab,注意 setContent 中的代碼是實現關鍵
        mTabHost.addTab(mTabHost.newTabSpec("tab_log")
                .setIndicator("日誌", getResources().getDrawable(R.drawable.login_bg))
                .setContent(new Intent(this, LogActivity.class)));

        // 添加應用設置的 tab
        mTabHost.addTab(mTabHost.newTabSpec("tab_setting")
                .setIndicator("設置", getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(new Intent(this, SettingActivity.class)));

        mTabHost.setCurrentTab(1);
    }
}

兩個子頁面各自對應一個 Activity 及其布局文件即可。透過 setIndicator 可以為標籤設置文字與圖標,透過 setCurrentTab 可指定默認顯示的標籤頁。

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