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

Android 選項卡(TabWidget)實作範例:從版面到清單的完整流程

編輯:Android 開發實例

本文整理一個可直接運行的 Android 選項卡範例,架構參考官方 SDK 文件中的 hello-tabwidget 教學,並為求簡潔省略了部分自製圖示,直接沿用系統內建資源。

實作步驟

1. 準備兩個 Activity 作為分頁內容

這裡先建立 OneActivity 與 TestActivity,各自用程式碼動態產生一個 TextView 當作畫面內容,省去額外配置檔的麻煩。

public class OneActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textview = new TextView(this);
        textview.setText("This is the Artists tab");
        setContentView(textview);
    }
}

2. 建立 tab.xml 描述整體版面

這個檔案決定了分頁長什麼樣子。有幾個地方一定要盯緊:

  • TabHost、TabWidget、FrameLayout 的 ID 必須分別是 @android:id/tabhost、@android:id/tabs、@android:id/tabcontent,一個字都不能改。
  • 外層 LinearLayout 必須設定 android:orientation="vertical",因為 LinearLayout 預設是水平排列。
  • 寬高屬性(layout_width / layout_height)也要照著寫,否則容易出現配置錯位。

如果執行後畫面和預期差很多,回頭檢查這幾點通常就能找到原因。

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

    <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">
        </TabWidget>

        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />

    </LinearLayout>
</TabHost>

3. 建立 TabWidget.java 並繼承 TabActivity

這個類別負責把版面與內容綁在一起。每個分頁透過 newTabSpec() 建立,再用 setIndicator() 設定標題與圖示,最後以 setContent() 指定內容來源——可以是 Intent,也可以是版面中的資源 ID。

package com.fatkun;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabWidget extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab); // 使用上一個步驟建立的版面

        Resources res = getResources();      // 用來取得 Drawable 資源
        TabHost tabHost = getTabHost();      // 取得 TabHost 容器
        TabSpec spec;
        Intent intent;                       // 每個分頁重複使用的 Intent

        // 第一個分頁
        intent = new Intent(this, OneActivity.class);
        spec = tabHost.newTabSpec("tab1")
                .setIndicator("Tab1", res.getDrawable(android.R.drawable.ic_media_play))
                .setContent(intent);         // 這裡也可以填 R.id.xxx
        tabHost.addTab(spec);

        // 第二個分頁
        intent = new Intent(this, TestActivity.class);
        spec = tabHost.newTabSpec("tab2")
                .setIndicator("Tab2", res.getDrawable(android.R.drawable.ic_menu_camera))
                .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(1);            // 預設停在第二個分頁
    }
}

4. 在 AndroidManifest.xml 註冊所有 Activity

主容器加上 android:theme="@android:style/Theme.NoTitleBar",可以隱藏標題列,把寶貴的垂直空間留給分頁內容;被當成分頁的 Activity 也必須逐一註冊,否則執行時會找不到元件。

<activity android:name="TabWidget"
    android:theme="@android:style/Theme.NoTitleBar"></activity>
<activity android:name="OneActivity"></activity>
<activity android:name="TestActivity"></activity>

重點回顧

這個範例最值得記住的觀念是:TabWidget 只是「頁籤外觀」,真正負責管理與切換的是 TabHost,而分頁內容的來源有兩種寫法——Intent 導向另一個 Activity,或直接指向同一版面中的某個 View。前者結構鬆散、易於擴充,是實務上較常採用的做法。

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