編輯:關於 Android 程式設計
TabWidget 能實現類似 Android 聯絡人那樣的多標籤切換效果,讓使用者透過點擊不同標籤顯示不同內容。要實現它,首先要認識 TabHost——一個用來容納多個 Tab 標籤的容器。每個 Tab 都能對應自己的佈局。
使用 TabHost 時,先透過 getTabHost() 取得物件,再用 addTab() 逐一加入 Tab。每個 Tab 在切換時都會產生事件,若要捕捉,可設定 setOnTabChangedListener 監聽器。
TabHost 內部需包含一個 TabWidget(顯示標籤頁)與一個 FrameLayout(顯示內容),並使用系統提供的專用 id:
<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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Linux"
android:textColor="#FF0000" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="MAC"
android:textColor="#385E0F" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Windows"
android:textColor="#1E90FF" />
</FrameLayout>
</LinearLayout>
</TabHost>
在 addTab() 中,透過 newTabSpec 建立每個標籤,以 setIndicator 設定標籤文字與圖示、setContent 指定對應的內容 View:
public class MainActivity extends TabActivity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
addTab(); // 添加標籤
tabHost.setBackgroundColor(Color.argb(150, 20, 80, 150)); // 設定背景顏色
tabHost.setBackgroundResource(R.drawable.ic_launcher); // 設定背景圖片
tabHost.setCurrentTab(0); // 設定預設顯示的標籤(索引從 0 開始)
// 標籤切換事件:從一個標籤切換到另一個時觸發
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示");
builder.setMessage("當前選中了" + tabId + "標籤");
builder.setPositiveButton("確定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.create().show();
}
});
}
// 為 TabHost 添加標籤
private void addTab() {
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("TAB1", getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.textview1));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("TAB2", getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.textview2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("TAB3", getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.textview3));
}
}



要特別留意的是,setOnTabChangedListener 監聽的是「標籤切換」事件(從一個標籤切到另一個),而非單純的點擊事件。掌握 TabHost、TabWidget 與 TabSpec 的配合,你就能輕鬆打造多頁籤的切換介面。
Android 代碼混淆實戰:ProGuard 規則怎麼寫才不會出事
一、概述 所謂代碼混淆(Obfuscated code),又稱花指令,指的是把程式碼轉換成功能等價但難以閱讀理解的形式。在 Android 專案中,這件事的操作面其實很集
Android自定義Button實現長按連續響應
本文為大家分享一個自定義Button實現長按連續響應的實例,供感興趣的讀者參考。 在電商類App的購物車頁面,常有這樣的需求:長按「+」或「-」按鈕時,商品數量能連續地增
Android UI 元件:LinearLayout 線性布局詳解
什麼是線性布局 LinearLayout 會把子元件排成一條線,可設定為垂直或水平。垂直時由上而下排列,水平時由左而右排列。它適合用於表單、按鈕列或簡單的資訊堆疊。 方
「開心學成語」App 實作全記錄:從建庫到 TabHost 介面
成語類 App 的特點是資料量大、結構固定,很適合用預置 SQLite 資料庫的方式來做。本文記錄一個成語學習應用的完整實作過程,包含資料庫打包、DAO 封裝、單元測試以