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

Android TabActivity 實作教學:打造多標籤頁畫面

編輯:Android 開發實例

前言

這段時間在研究 Android 平台上的開源專案 StandupTimer——這是一個由 jwood 設計、用來控制會議時間的簡易應用,類似秒表倒數。藉由它來說明 TabActivity 的用法再合適不過。

TabActivity 與 TabHost

TabActivity 繼承自 Activity,內部已定義好 TabHost,可透過 getTabHost() 取得。TabHost 含有兩種子元素:可自由選擇的 Tab,以及對應的內容 tabContent;在版面配置的 <TabHost> 底下,它們分別對應 TabWidget 與 FrameLayout。使用 TabActivity,能讓同一個畫面容納更多內容。本文以 StandupTimer 的 TeamDetailsActivity 為例,它包含兩個 Tab:一個顯示團隊統計資訊,一個顯示團隊參與的會議列表(一個 ListView)。

建立版面配置

不論用 TabActivity 還是自訂 TabHost,都要求以 TabHost 作為 XML 版面配置的根節點。

<?xml version="1.0" encoding="utf-8"?>
<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: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">
 <!-- 省略部分程式碼 -->
 <TextView android:id="@+id/no_team_meetings"
 android:textSize="18sp" android:layout_width="fill_parent"
 android:layout_height="fill_parent" />
 <TextView android:id="@+id/no_team_meeting_stats"
 android:textSize="18sp" android:layout_width="fill_parent"
 android:layout_height="fill_parent" />
 </FrameLayout>
 </LinearLayout>
</TabHost>

一般採用線性版面配置,所以 <TabHost> 的子元素是 <LinearLayout>。<TabWidget> 對應 Tab,<FrameLayout> 則用來容納各 Tab 要顯示的內容。要特別注意的是,<TabWidget> 與 <FrameLayout> 的 id 必須使用系統 id,分別為 android:id/tabs 與 android:id/tabcontent,因為系統會用這兩個 id 來初始化 TabHost 的兩個實例變數(mTabWidget 與 mTabContent)。

撰寫 Java 程式碼

建立標籤頁有兩種方式:一是繼承 TabActivity,再用 getTabHost() 取得 TabHost;二是自訂 TabHost,在版面配置的 <TabHost> 上自訂 id,再用 findViewById() 取得。本文採用前者。

private void createTabs() {
 TabHost tabhost = getTabHost();
 tabhost.addTab(tabhost.newTabSpec("stats_tab").
 setIndicator(this.getString(R.string.stats)).
 setContent(createMeetingDetails(team)));

 tabhost.addTab(tabhost.newTabSpec("meetings_tab").
 setIndicator(this.getString(R.string.meetings)).
 setContent(createMeetingList()));
 getTabHost().setCurrentTab(0);
}

首先要取得 TabHost 物件,可透過 TabActivity 的 getTabHost()。若是自訂 TabHost,在加入 Tabs 前應先呼叫 setUp():

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

SDK 原文也強調:用 findViewById() 載入 TabHost 時要先呼叫 setup();但用 TabActivity 的 getTabHost() 則不需要。

接著呼叫 tabHost.addTab(TabSpec) 加入各個標籤頁。TabSpec 主要透過 setIndicator 與 setContent 來指定 Tab 與其內容,並以 newTabSpec(String tag) 建立實例。setIndicator() 有三個重載:可同時指定標題與圖示、以自訂 View 定義、或只指定標題(無圖示)。setContent() 也有三種:以 TabContentFactory 產生、以 ViewId 指定顯示內容、或以 Intent 直接載入新的 Activity。

本例透過 TabContentFactory 來指定對應的 TabContent。它是個只含一個 createTabContent(String tag) 回傳 View 方法的介面:

private TabHost.TabContentFactory createMeetingDetails(Team team2) {
 return new TabHost.TabContentFactory() {
 @Override
 public View createTabContent(String tag) {
 setStatsTabContent();
 return findViewById(R.id.teamStats);
 }
 };
}

也可以讓 TabActivity 直接實作 TabContentFactory 介面(例如 class Tabs2 extends TabActivity implements TabHost.TabContentFactory),再在類別中實作 createTabContent 方法回傳對應的 View。

最後把 TabSpec 加入 TabHost,即 tabHost.addTab(tabSpec)。由於 setIndicator 與 setContent 都回傳 TabSpec 自身,因此可以用串接方式撰寫:

tabhost.addTab(tabhost.newTabSpec("stats_tab")
 .setIndicator(this.getString(R.string.stats))
 .setContent(createMeetingDetails(team)));
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved