編輯:關於 Android 程式設計
TabHost 可以在同一個界面裡容納多個選項卡,很多應用都用它來做頂部或底部的分欄切換。本文從兩個基礎概念講起,再走一個可運行的最小範例。
TabWidget:就是 TabHost 上方或下方那一排可點擊的按鈕,負責觸發選項卡切換。
TabSpec:代表一個選項卡界面,newTabSpec(String tag) 建立,addTab(tabSpec) 加入 TabHost。
TabHost 的基本用法有兩個要點:佈局中的 id 必須使用系統預定義值,Activity 則繼承 TabActivity。
main.xml
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="aa" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="bb" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Main.java
package com.app.main;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Main extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabHost = this.getTabHost();
TabSpec tab1 = tabHost.newTabSpec("tab1")
.setIndicator("tab1")
.setContent(R.id.tab1);
tabHost.addTab(tab1);
TabSpec tab2 = tabHost.newTabSpec("tab2")
.setIndicator("tab2")
.setContent(R.id.tab2);
tabHost.addTab(tab2);
}
}
實現效果:

若想在使用者點擊 TabWidget 時執行額外邏輯,有兩種常見做法。
做法一,監聽整個 TabHost,回調參數中的 id 就是 TabSpec 的 tag(此例為 "tab1"、"tab2"):
tabHost.setOnTabChangedListener(new TabChangeListener() {
public void onTabChanged(String id) {
}
});
做法二,直接拿到 TabWidget 的某個子 View 綁定點擊:
tabWidget.getChildAt(0).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
兩者的差別在於粒度:setOnTabChangedListener 關心的是「切換到了哪個 Tab」,而 getChildAt(i).setOnClickListener 能針對單個標籤按鈕做更細緻的交互處理。
深入解析 Android 布局中的 xmlns:tools 命名空間
前言 寫 Android 版面時,Android Studio 右側的預覽視窗很方便,但有個尷尬之處:有些文字是寫在 Activity 的 Java 程式碼裡才設上去的
RxJava 過濾類操作符整理:debounce、filter、take、skip 十式
這篇把 RxJava 中常見的「過濾」家族操作符做個集中整理,每一項都附上最小可運行的範例與說明。 一、debounce 只有在一段指定的時間內沒有再發射新資料時,才把
Java/Android 設計模式筆記(11):原型模式 Prototype
本文要談的是最後一個建立型模式——原型模式(Prototype Pattern)。這個模式先準備一個樣板物件,再由樣板複製出內部屬性一致的執行個體,這個複製的動作在 C+
無線點餐系統資料庫設計:MySQL 表結構規劃
無線點餐系統的資料庫設計,需要涵蓋使用者、餐桌、菜單分類、菜單、訂單與訂單明細。 主要資料表 usertbl 保存帳號、密碼與權限;tabletbl 記錄餐桌狀態;men