編輯:Android 開發實例
本文整理 android.widget.TabWidget 的 API 說明,並搭配屬性表、方法清單與可運行的範例程式碼,方便查閱與對照實作。
public class TabWidget extends LinearLayout
implements View.OnFocusChangeListener
java.lang.Object
└─ android.view.View
└─ android.view.ViewGroup
└─ android.widget.LinearLayout
└─ android.widget.TabWidget
TabWidget 負責顯示一列選項卡標籤,每個標籤對應父容器 TabHost 中的一頁。當使用者點選某個標籤時,TabWidget 會通知父容器 TabHost 切換到該頁顯示。
實務上要特別注意分工:多數操作並不會直接呼叫在 TabWidget 上。新增標籤、管理回呼這些工作,都應該透過容器 TabHost 來完成。你通常只會用 TabWidget 做兩件事——走訪現有的標籤清單,或微調標籤列的視覺呈現。
| 屬性名稱 | 說明 |
|---|---|
android:divider | 指定在標籤之間繪製的分隔物件。 |
android:tabStripEnabled | 決定是否繪製標籤底部的分隔線。 |
android:tabStripLeft | 用於繪製標籤下方分隔線左半段的物件。 |
android:tabStripRight | 用於繪製標籤下方分隔線右半段的物件。 |
新增子檢視。若子檢視未設定版面參數,系統會套用此 ViewGroup 的預設參數。
當 addStatesFromChildren() 回傳 true 時,重新整理此群組(含子元素)的視覺狀態。
繪製流程中負責畫出子檢視。衍生類別若想在自身內容繪製前取得控制權,可覆寫此方法。
將焦點移到指定索引的標籤並使其被選取。與 setCurrentTab() 的差別在於:後者只切換選取狀態、不搶焦點;前者則兩者都做。透過 UI 操作時,焦點會由 TabWidget 的 onFocusChanged() 自動處理;但如果你是用程式碼控制切頁,就得自己確保焦點狀態一致。
取得指定索引位置上的標籤檢視,索引從 0 開始。
回傳目前標籤的數量。
回報標籤底部的分隔線目前是否處於繪製狀態。
檢視焦點狀態改變時的回呼。
設定目前的選項標籤,並通知其他 UI 元素切換。這裡的「選取」是視圖邏輯層面的概念,與「焦點」不同:例如在分頁內的清單中上下捲動,UI 焦點會移動,但並不會改變分頁的選取狀態——只有切換分頁時,選取狀態才會更新。若需要同時完成選取與取焦,請改用 focusCurrentTab()。
設定標籤之間的分隔物件,可傳入 Drawable 或資源 ID。
設定檢視的啟用狀態,實際意義依子類別而異。
分別設定標籤下方分隔線的左半段與右半段外觀,同樣各有 Drawable 與資源 ID 兩種多載。
控制標籤底部分隔線是否繪製,預設為繪製。若你為標籤指定了自訂檢視,TabHost 會呼叫此方法關閉底線繪製。
getChildDrawingOrder(int childCount, int i):回傳本次迭代中應繪製的子元素索引,可覆寫以調整繪製順序,預設直接回傳 i。要讓此方法生效,必須先呼叫 setChildrenDrawingOrderEnabled(true)。
onSizeChanged(int w, int h, int oldw, int oldh):檢視尺寸變更時的回呼,四個參數分別為目前寬高與先前的寬高。若檢視是動態加入階層的,先前值可視為 0。
以下範例展示三個分頁,並示範如何切換 TabWidget 的顯示狀態與分隔線狀態。
public class TabDemo1 extends Activity implements OnClickListener {
TabHost mTabHost = null;
TabWidget mTabWidget = null;
Button btnVisiable, btnStrip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabdemo1);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
mTabWidget = mTabHost.getTabWidget();
mTabHost.addTab(mTabHost.newTabSpec("tab1")
.setContent(R.id.LinearLayout001).setIndicator("Tab1"));
mTabHost.addTab(mTabHost.newTabSpec("tab2")
.setContent(R.id.LinearLayout002).setIndicator("Tab2"));
mTabHost.addTab(mTabHost.newTabSpec("tab3")
.setContent(R.id.LinearLayout003).setIndicator("Tab3"));
btnVisiable = (Button) findViewById(R.id.btnVisiable);
btnStrip = (Button) findViewById(R.id.btnStrip);
btnVisiable.setOnClickListener(this);
btnStrip.setOnClickListener(this);
setTitle("共有 " + mTabWidget.getTabCount() + " 個 tab");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnVisiable:
if (mTabWidget.getVisibility() != android.view.View.VISIBLE) {
mTabWidget.setVisibility(android.view.View.VISIBLE);
} else {
mTabWidget.setVisibility(android.view.View.INVISIBLE);
}
break;
case R.id.btnStrip:
mTabWidget.setStripEnabled(!mTabWidget.isStripEnabled());
mTabWidget.setRightStripDrawable(android.R.color.transparent);
break;
default:
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost">
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical"
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_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout android:id="@+id/LinearLayout001"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btnVisiable"
android:text="顯示狀態切換"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
<Button android:id="@+id/btnStrip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="分隔線狀態切換"></Button>
<TextView android:id="@+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一個分頁" />
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout002"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/TextView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第二個分頁" />
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout003"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/TextView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第三個分頁" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Android MediaPlayer 範例:建立可控制音訊的播放器
這個範例會建立一個可播放、暫停、快轉與快退的音訊播放器。畫面中會包含播放控制按鈕、時間顯示與 SeekBar 進度條。 使用 raw 資源 將 song.mp3 放入 r
Android JSON 解析器:用 JSONObject 提取所需資料的完整流程
JSON 代表 JavaScript Object Notation,是一種獨立的資料交換格式,也是 XML 的最佳替代品。本章說明如何解析 JSON 檔案,並從中取出需
以百度地圖 SDK 實現 Android 定位功能:完整流程解析
LBS 位置服務是 Android 應用中愈來愈常見的能力,從定位、地圖顯示到導航,都建立在它之上。本文以百度地圖定位 SDK 為例,逐步示範如何完成一次定位並把結果回傳
Android 日曆專案實作(二):建立專案與 ADT 開發環境操作
接續上一篇,這次從建立專案開始,把 ADT 中幾個常用但容易被忽略的操作記錄下來。 一、建立 Android Project 在新建專案對話框中填入 App 名稱與套件