編輯:關於 Android 程式設計
想讓 App 有「電話簿」那種分頁切換的視覺效果嗎?Android 內建的 TabHost 搭配 TabWidget,是最容易上手的一條路。本文透過一個可直接運行的小範例,把整個流程完整走一遍。
TabHost 是承載多個分頁的容器,而每一個分頁都可以掛上屬於自己的版面配置——電話簿裡的那些分頁,本質上就是一個個 LinearLayout。負責畫出上方那排可點擊標籤的是 TabWidget,真正用來替換內容的則是 FrameLayout。
使用順序相当固定:先用 getTabHost() 取得 TabHost 物件,再用 addTab() 把分頁一個個塞進去。每個 TabSpec 透過 setIndicator() 設定標題文字與圖示,再以 setContent() 綁定要顯示的 View。如果想在切換時收到通知,只要掛上 setOnTabChangedListener 就行。
package org.hualang.tab;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;
public class TabTest extends TabActivity {
TabHost tabhost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得 TabHost 物件
tabhost = getTabHost();
// 新增分頁:newTabSpec 建立規格,setIndicator 設定文字與圖示,setContent 綁定內容
tabhost.addTab(tabhost.newTabSpec("tab1")
.setIndicator("TAB 1", getResources().getDrawable(R.drawable.img1))
.setContent(R.id.text1));
tabhost.addTab(tabhost.newTabSpec("tab2")
.setIndicator("TAB 2", getResources().getDrawable(R.drawable.img2))
.setContent(R.id.text2));
tabhost.addTab(tabhost.newTabSpec("tab3")
.setIndicator("TAB 3", getResources().getDrawable(R.drawable.img3))
.setContent(R.id.text3));
// 也可以改背景色,或改用背景圖片資源
// tabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));
tabhost.setBackgroundResource(R.drawable.bg0);
// 指定啟動時顯示哪一個分頁(索引從 0 開始)
tabhost.setCurrentTab(0);
// 監聽分頁切換事件
tabhost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
Toast toast = Toast.makeText(getApplicationContext(),
"現在是" + tabId + "標簽", Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
<?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/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="選項卡1" />
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="選項卡2" />
<TextView
android:id="@+id/text3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="選項卡3" />
</FrameLayout>
</LinearLayout>
</TabHost>
這段配置裡有三個 id 是寫死的,必須完全照抄:@android:id/tabhost、@android:id/tabs 與 @android:id/tabcontent。任何一個打錯,TabHost 就會在執行期找不到對應的檢視而拋出例外。
另外要留意的是,TabActivity 這種寫法在後續版本中被視為舊式做法;若你在新專案中使用,建議改用 Fragment 搭配 TabLayout 來達成同樣的效果,但底層的分頁切換邏輯與這裡描述的觀念完全一致。
以上就是 TabHost 與 TabWidget 的完整實作流程,希望對你的 Android 開發有所幫助。
【自定義 View 系列】04:Touch 事件分發機制全解析
本篇分三個層次展開:先看 View 如何處理 Touch,再看 ViewGroup 的事件分發,最後討論滑動衝突的解法。 閱讀源碼時我習慣邊看邊加註釋,以下把關鍵方法提取
Android進階教學:Splash啟動頁的版本更新業務邏輯
本單元聚焦於Android進階開發中常見的Splash(啟動引導頁)設計,重點在於梳理啟動頁在「版本更新」情境下的業務判斷流程:應用啟動時,Splash頁會先確認目前的版
獲取 Android 手機 CPU 類型:ARM、ARMV7 與 NEON 判斷
在 Android 開發中,特別是涉及 Native 庫或影音解碼時,判斷手機 CPU 的架構與指令集至關重要。本文介紹如何透過命令列與程式碼兩種方式獲取 CPU 資訊。
Android 多語言資源建置:讓 App 自動切換語言與字型
要在 Android 專案中支援多語言,靠的是資源目錄的「限定符(qualifier)」機制:系統會依裝置目前的語言地區,自動挑選最相符的一組資源。開發者不需要寫任何判斷