編輯:關於 Android 程式設計
網路上關於 Android 選項卡的教學品質參差不齊,常有漏步驟或版本混用的情況。以下整理一份可完整跑通的 TabHost 實作流程,概念清楚、可直接照抄。
這個範例共需三個 Activity:一個作為容器的 TabActivity(MainActivity),另外兩個是各選項卡對應的內容頁(TabOne、TabTwo)。三者都必須在 manifest 中註冊。
<activity
android:name="com.example.android_tabname.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.android_tabname.TabOne" />
<activity android:name="com.example.android_tabname.TabTwo" />
MainActivity 的版面必須以 TabHost 為根節點,而且它的 id 一定要使用系統內建的 @android:id/tabhost,這不是風格問題而是硬性要求——TabActivity 就是靠這個 id 去找控制器的。同理,TabWidget 對應 @android:id/tabs,內容容器 FrameLayout 對應 @android:id/tabcontent。
<?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="match_parent"
android:layout_height="match_parent">
<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" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</TabHost>
最外層的 LinearLayout 方向必須設為 vertical,否則標籤列與內容區會疊在一起。
兩個內容頁的版面相同,這裡只列一份:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="This is TabTwo"
android:textSize="50dip" />
</LinearLayout>
流程固定為四步:取得 TabHost 實例 → 用 newTabSpec() 建立 TabSpec → 呼叫 setIndicator() 設定標籤文字與圖示、setContent() 綁定內容 → 最後 addTab() 加入容器。
package com.example.android_tabname;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
private TabHost tabHost;
private TabHost.TabSpec tabSpecA, tabSpecB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();
// 第一個選項卡
tabSpecA = tabHost.newTabSpec("One");
tabSpecA.setIndicator("TabA",
getResources().getDrawable(android.R.drawable.ic_media_play));
Intent intentA = new Intent(this, TabOne.class);
tabSpecA.setContent(intentA);
// 第二個選項卡
tabSpecB = tabHost.newTabSpec("Two");
tabSpecB.setIndicator("TabB",
getResources().getDrawable(android.R.drawable.ic_media_next));
Intent intentB = new Intent(this, TabTwo.class);
tabSpecB.setContent(intentB);
// 加入 TabHost
tabHost.addTab(tabSpecA);
tabHost.addTab(tabSpecB);
}
}
實務建議:選項卡數量固定時,為每個 TabSpec 配置獨立變數即可;數量動態變化時再改用集合管理,避免多個 TabSpec 共用同一個參照而互相覆寫。
這兩個類別目前只負責載入版面,後續可依需求擴充各自的邏輯。
package com.example.android_tabname;
import android.app.Activity;
import android.os.Bundle;
public class TabOne extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_tabone);
}
}
package com.example.android_tabname;
import android.app.Activity;
import android.os.Bundle;
public class TabTwo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_tabtwo);
}
}
TabActivity 在 API 13 之後已被標記為老舊寫法,官方推薦改用 FragmentActivity 搭配 TabLayout 與 ViewPager。不過理解 TabHost 的「容器 + TabSpec + 內容 Intent」這層模型仍有價值,遷移到 Fragment 版本時,概念幾乎可以一比一對應:TabSpec 變成 TabLayout 的 Tab,內容 Intent 變成 Fragment。
Android 百度地圖開發系列:地圖初始化與定位功能實作
在 Android 應用中,地圖功能的使用頻率相當高。回顧過去寫過的專案和用過的百度地圖 API,這裡整理一份從零開始實作 Android 百度地圖的完整流程,第一篇聚焦
TextView 跑馬燈效果的兩種實作方式
當文字過長而寬度不足時,跑馬燈是很常見的解法。Android 的 TextView 本身支援 marquee 效果,但若要讓它持續滾動,還需要處理焦點問題。 方法一:X
Android 實作兩層下拉選單與即時搜尋
本文示範如何實作一個「上方搜尋框 + 下方兩層下拉選單」的互動效果,供大家參考。 一、目標效果 上方是搜尋框,下方是可展開的兩層下拉選單。 輸入關鍵字後,下方列表會
Win10 UWP 自定義 Marquez 進度條:文字與進度同步移動
在 Win10 UWP 應用中,若想要一個不只是單純長條的進度條,而是搭配文字並讓文字隨進度移動,可以自定義控制項來完成。這種效果常用於遊戲掛機、任務進度或載入提示。