Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 技術基礎

TabHost 標籤頁實作:用分頁切換不同內容

編輯:Android 技術基礎

TabHost 的作用類似桌面應用程式裡的「分頁」或「選項卡」:點一下標籤,下方內容就跟著換。Android 提供兩種實作路徑,一種是讓 Activity 繼承 TabActivity,另一種則完全用版面配置檔搞定。

  • 做法一:繼承 TabActivity,用 getTabHost() 取得 TabHost 物件,各分頁的內容在版面配置檔中定義。
  • 做法二:不繼承 TabActivity,直接在版面配置檔中宣告 TabHost。此時有兩個 id 是硬性規定:TabWidget 的 id 必須是 @android:id/tabs,FrameLayout 的 id 必須是 @android:id/tabcontent。

一、做法一:繼承 TabActivity

1. 版面配置

開啟 res/layout/activity_main.xml。內容區用 FrameLayout 疊放三個 LinearLayout,每個 LinearLayout 就是一個分頁的內容:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 分頁一:文字新聞 -->
    <LinearLayout android:id="@+id/txtnews"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <RatingBar
            android:id="@+id/ratingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <!-- 分頁二:圖片新聞 -->
    <LinearLayout android:id="@+id/photonews"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <AnalogClock android:id="@+id/clock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <!-- 分頁三:視頻新聞 -->
    <LinearLayout android:id="@+id/videonews"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ToggleButton" />
    </LinearLayout>
</FrameLayout>

2. 程式實作

public class MainActivity extends TabActivity {

    private TabHost tabNews;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 取得 TabActivity 內建的 TabHost
        tabNews = this.getTabHost();

        // inflate 的意思,通俗地說就是把 XML 定義的版面找出來並掛上去
        LayoutInflater.from(this).inflate(
                R.layout.activity_main, tabNews.getTabContentView(), true);

        // 分頁一
        tabNews.addTab(tabNews.newTabSpec("firsttab")
                .setIndicator("文本新聞", getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(R.id.txtnews));

        // 分頁二
        tabNews.addTab(tabNews.newTabSpec("secondtab")
                .setIndicator("圖片新聞", getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(R.id.photonews));

        // 分頁三
        tabNews.addTab(tabNews.newTabSpec("thirdtab")
                .setIndicator("視頻新聞", getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(R.id.videonews));
    }
}

這串程式碼在做的事情可以拆成三層來理解:newTabSpec() 建立一個分頁規格並命名,setIndicator() 設定標籤上的文字與圖示,setContent() 則指定這個分頁要顯示哪個 View——傳入的是版面配置中某個元件的 id,TabHost 會把它從 FrameLayout 裡搬到自己的內容區。順序不能顛倒,因為 indicator 與 content 都必須掛在同一個 TabSpec 上。

3. 執行結果

點選上方標籤,下方內容便在評分星等、時鐘與切換按鈕之間切換。

二、實務上的提醒

TabActivity 與 TabHost 在後續版本中已被標記為 deprecated,原因是 TabActivity 這種「Activity 裡塞 Activity」的設計與後來的 Fragment 架構不合。新專案若要達成分頁效果,建議改用 ViewPager(或 ViewPager2)搭配 TabLayout,以 Fragment 作為每一頁的內容單元;這樣做不僅符合現代 Android 的แนวคิด,也能在旋轉螢幕與生命週期管理上少踩很多坑。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved