編輯:Android 開發實例
Tab 是行動介面裡最直覺的分頁方式,能把大量內容收進同一個螢幕,讓使用者左右切換而不必層層進入。代價是開發複雜度上升,所以建議真的有必要時再採用。
Android 的 Tab 實作有個讓新手困惑的地方:XML 結構必須嚴格遵守三層巢狀與特定 id 命名,少一層或名字寫錯就會在執行期爆炸。
順序與命名都不能改:

TabHost → TabWidget(id 必須是 @android:id/tabs)→ FrameLayout(id 必須是 @android:id/tabcontent)。這兩個 id 是寫死在框架裡的,自訂名稱不會被辨識。
先看一下執行結果:

<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@android:id/TabHost1">
<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:paddingTop="65px"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_height="wrap_content"
android:id="@+id/Tab1"
android:orientation="vertical"
android:layout_width="fill_parent">
<EditText android:layout_height="wrap_content"
android:id="@+id/edtTab1"
android:layout_width="fill_parent"></EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnTab1" android:text="Tab1"></Button>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:id="@+id/Tab2"
android:layout_width="fill_parent"
android:orientation="horizontal">
<EditText android:layout_height="wrap_content"
android:id="@+id/edtTab2"
android:layout_width="wrap_content"
android:layout_weight="300"></EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnTab2" android:text="Tab2"></Button>
</LinearLayout>
</FrameLayout>
</TabHost>
注意 FrameLayout 裡每個子 LinearLayout 都有自己的 id,這個 id 稍後會被當作 Tab 的內容指向。
Activity 要繼承 TabActivity,透過 getTabHost() 取得容器,再用 newTabSpec() 一個一個把分頁掛上去。
package com.testTab;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class testTab extends TabActivity {
Button btnTab1, btnTab2;
EditText edtTab1, edtTab2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabs = getTabHost();
// 設定 Tab1
TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setIndicator("tab1"); // 分頁標題文字
tab1.setContent(R.id.Tab1); // 關聯的容器 id
tabs.addTab(tab1);
btnTab1 = (Button) this.findViewById(R.id.btnTab1);
edtTab1 = (EditText) this.findViewById(R.id.edtTab1);
btnTab1.setOnClickListener(new ClickEvent());
// 設定 Tab2
TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setIndicator("tab2");
tab2.setContent(R.id.Tab2);
tabs.addTab(tab2);
btnTab2 = (Button) this.findViewById(R.id.btnTab2);
edtTab2 = (EditText) this.findViewById(R.id.edtTab2);
btnTab2.setOnClickListener(new ClickEvent());
tabs.setCurrentTab(0); // 預設停在第一頁
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
if (v == btnTab1) {
edtTab1.setText("tab1");
} else if (v == btnTab2) {
edtTab2.setText("tab2");
}
}
}
}
setIndicator() 除了傳字串,也可以傳 (icon, title) 的重載版本來顯示圖示。
若想用自訂圖當分頁標籤並做出選中/未選中的視覺差異,需要準備 selector 的 state_list drawable,透過 tab1.setIndicator(R.drawable.tab_icon) 指定。
最後要說的是:TabActivity 與 TabHost 都已被標記為 deprecated。新專案建議改用 TabLayout 搭配 ViewPager2 與 Fragment,API 更直覺、行為也更符合 Material Design 規範。上面這套寫法主要價值在於理解 Tab 背後「容器切換」的思維,以及維護老專案時用得上。
Android MediaPlayer:打造可播放、暫停與快轉的音樂播放器
Android 提供了多種控制音訊/影片播放的方式,其中最常用的是 MediaPlayer 類別。 MediaPlayer 讓我們存取系統內建的媒體播放服務來播放音訊、
Android底部菜單欄實戰:TabHost打造帶未讀紅點的通用底欄
上一個項目收尾後,這周難得清閒,我便把過去的項目翻出來整理,想把一些通用模塊封裝起來,以後遇到相似需求就不用反覆造輪子了,也能省下不少開發時間。今天把這個 demo 貼出
Android 開發實作:打造仿支付寶錢包的九宮格 GridView
本文要帶大家實作一個仿支付寶錢包首頁、每個格子之間帶有分割線的 GridView,也就是俗稱的九宮格效果。先來看看成品長什麼樣子:我們都知道,ListView 要加分割線
Gallery 搭配 ImageSwitcher:打造可滑動切換的看圖元件
上一篇談了 Gallery 的基礎用法,這次把它跟 ImageSwitcher 組合起來,做出一個真正好用的看圖畫面。 為什麼要兩個控件一起用?因為 Gallery 的