編輯:Android 開發實例
這段時間在研究 Android 平台上的開源專案 StandupTimer——這是一個由 jwood 設計、用來控制會議時間的簡易應用,類似秒表倒數。藉由它來說明 TabActivity 的用法再合適不過。
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)。
建立標籤頁有兩種方式:一是繼承 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)));
Android 進度提示元件:Spinner 與 ProgressBar 的實作方式
在 Android 上,若要向使用者傳達「工作進行中」,最直接的方式就是顯示進度條。ProgressBar 有兩種外觀:可標示百分比的長條式進度條,以及旋轉式的 spin
Android JSON 資料解析:JSONObject 與天氣資訊抓取
在 Android 開發中,JSON 解析幾乎是網路應用的必備技能。無論是氣象、新聞、社群或商品資料,後端常會以 JSON 格式回傳結果。本文說明如何從 JSON 中取出
Android 自訂 ActionBar 的實作方式
自訂 ActionBar 的概念 在 Android 3.0 之前,若需要類似 ActionBar 的頂部操作列,可自訂元件或使用開源方案。這類元件通常包含標題、首頁圖示
Android 載入 Spinner 教學:用轉圈進度提示未知工作
在 Android 中,當工作耗時未知時,可以使用不確定模式的進度條,也就是常說的加載 Spinner。它會持續轉動,提醒使用者系統正在處理中。 加入 Spinner 在