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

深入 TabHost:從原始碼看它的三個必要元件

編輯:Android 開發實例

TabHost 讓你可以在同一個螢幕裡切換不同版面,Android 自帶的撥號程式就是最直白的例子:通話紀錄、聯絡人、撥號鍵盤共用一個頁面,靠頁籤切來切去。

原始碼告訴你的硬性規定

翻開 TabHost 的原始碼,主要的成員變數只有三個:

private TabWidget mTabWidget;
private FrameLayout mTabContent;
private List<TabSpec> mTabSpecs;

這也意味著你的 XML 佈局必須提供 TabWidget 與 FrameLayout,因為建構過程會直接檢查,缺了就丟例外:

if (mTabWidget == null) {
    throw new RuntimeException(
        "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
}

mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
if (mTabContent == null) {
    throw new RuntimeException(
        "Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'");
}

至於 mTabSpecs,它就是你註冊的頁籤清單,初始容量給 2,不夠會自動擴充:

private List<TabSpec> mTabSpecs = new ArrayList<TabSpec>(2);

準備每個頁籤的內容

先寫兩個內容佈局。tab1.xml 用 LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView android:text="tab1 with linear layout"
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

tab2.xml 則是 FrameLayout 裡再包一層 LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout android:id="@+id/LinearLayout02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView android:text="tab2"
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</FrameLayout>

路線一:沿用系統佈局

繼承 TabActivity,直接呼叫 getTabHost(),它內部做的就是:

if (mTabHost == null) {
    this.setContentView(com.android.internal.R.layout.tab_content);
}

容器有了,接著 inflate 內容、註冊 TabSpec:

TabHost mTabHost = getTabHost();
LayoutInflater inflater_tab1 = LayoutInflater.from(this);
inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView());
inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());

mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
        .setIndicator("TAB 11").setContent(R.id.LinearLayout01));
mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
        .setIndicator("TAB 11").setContent(R.id.FrameLayout02));

注意這種寫法不需要自己呼叫 setContentView(),因為 getTabHost() 已經代你做完了。

路線二:自己寫 TabHost 佈局

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+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">
        </FrameLayout>
    </LinearLayout>
</TabHost>

除了 TabHost 自己的 id 可以自訂,其餘必須沿用系統 id——原因就在前面那段原始碼裡。取得元件的方式改成 findViewById,並且一定要補上 setup():

setContentView(R.layout.main);
TabHost mTabHost = (TabHost) findViewById(R.id.tabhost);
mTabHost.setup();

LayoutInflater inflater_tab1 = LayoutInflater.from(this);
inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView());
inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());

mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
        .setIndicator("TAB a").setContent(R.id.LinearLayout01));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
        .setIndicator("TAB b").setContent(R.id.FrameLayout02));

官方註解把這件事說得很明白:用 findViewById 載入 TabHost 時,加頁籤前必須先呼叫 setup();但透過 TabActivity 的 getTabHost() 取得則不需要。setup 的作用正是把 mTabWidget 與 mTabContent 這兩個成員初始化好,這也正好解釋了為什麼那兩個 id 一定要用系統定義的。

兩條路線的執行結果相同,差別只在於你要不要自己維護那份佈局檔。

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