Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實作:把 TabHost 標籤列移到畫面最下方

編輯:關於 Android 程式設計

Android 早期的 TabActivity 預設把標籤列放在頂端,但多數現代應用的底部導覽列設計更符合拇指操作習慣。要改位置,關鍵在於理解 TabHost 內部由 TabWidget 與 FrameLayout 組成的結構,並用 LinearLayout 的垂直排列重新安排順序。

一、版面配置

核心觀念:把 TabWidget 寫在 FrameLayout 之後,這樣在垂直 LinearLayout 中它就會落在下方。FrameLayout 加上 layout_weight="1" 佔滿剩餘空間,標籤列則以 wrap_content 貼齊底部。

<?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:id="@+id/ll_main_tab"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <!-- 內容區:佔滿上方剩餘空間 -->
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:gravity="center"
            android:fadingEdge="none"
            android:fadingEdgeLength="0.0dip"
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="1.0">

            <RelativeLayout
                android:id="@+id/tabFirst"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <AnalogClock
                    android:id="@+id/widget31"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tabSecond"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <DigitalClock
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true" />
            </RelativeLayout>
        </FrameLayout>

        <!-- 標籤列:放在內容區之後,就會顯示在下方 -->
        <TabWidget
            android:id="@android:id/tabs"
            android:background="@drawable/tab_btn_bg_normal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</TabHost>

這裡有幾個 id 必須嚴格遵守:@android:id/tabhost、@android:id/tabcontent、@android:id/tabs。這三個是 TabHost 內部以程式碼尋找子元件時使用的固定識別碼,寫錯任何一個都會在執行時拋出空指針例外。這也是自訂 TabHost 版面最常見的失敗原因。

二、Activity 程式碼

package com.tanghy.tab.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class MainActivity extends TabActivity {

    TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabHost = getTabHost();

        // 使用版面中的 View id 作為內容
        tabHost.addTab(tabHost.newTabSpec("one")
                .setIndicator("aaaa")
                .setContent(R.id.tabFirst));

        tabHost.addTab(tabHost.newTabSpec("two")
                .setIndicator("bbb")
                .setContent(R.id.tabSecond));

        // 使用 Intent 跳轉到另一個 Activity 作為內容
        tabHost.addTab(tabHost.newTabSpec("three")
                .setIndicator("ccc")
                .setContent(new Intent().setClass(this, LogActivity.class)));

        tabHost.setCurrentTab(0);
    }
}

從程式碼可以看出,setContent() 支援兩種寫法:傳入版面中的 View id(如上面前兩個),或傳入一個 Intent(如第三個,會啟動對應的 Activity 並把它的畫面嵌進內容區)。後者更模組化,適合每個分頁都是獨立功能頁的情況。

三、實務上的重要提醒

  • 這套做法已經過時。TabActivity 自 API 13 起就被標記為 deprecated,官方建議改用 Fragment 搭配 ViewPager,或直接採用 BottomNavigationView、Material Design 的 TabLayout。新專案請不要再用 TabActivity。
  • 標籤樣式:範例中的 setIndicator("aaaa") 只設定了純文字。若要顯示圖示,可改用 setIndicator("文字", Drawable) 的重載版本,或傳入自訂的 View。
  • 標籤高度與文字居中:TabWidget 預設文字是靠底部對齊的,若要調整,需取得 getTabWidget() 後遍歷子 View 修改 LayoutParams,這部分可參考 TabWidget 樣式調整的相關做法。
  • modern 替代方案:若只是想要底部導覽列,BottomNavigationView 幾行 XML 就能完成,並自動處理切換動畫、選中狀態與無障礙標籤,程式碼量遠少於上述範例。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved