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

把 Tab 選單移到螢幕下方:Android 底部導覽列的最簡實作

編輯:關於 Android 程式設計

新浪微博、京東、淘寶這類 App,都把選項卡放在螢幕最下方當作主框架,但 Android 的 TabWidget 預設是長在上方的。網路上做法很多,這裡分享一個改動最少、最容易理解的版本。

核心原理只有一行

把 TabWidget 放進 RelativeLayout,然後加上 android:layout_alignParentBottom="true",標籤列就會乖乖貼齊螢幕底部,剩下的 FrameLayout 自動佔滿上方空間。

版面配置:main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost android:id="@+id/tabhost"
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        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"
            android:layout_alignParentBottom="true" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">
                <TextView android:id="@+id/view1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/textView_1" />
            </LinearLayout>

            <LinearLayout android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">
                <TextView android:id="@+id/view2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/textView_2" />
            </LinearLayout>

            <LinearLayout android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">
                <TextView android:id="@+id/view3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/textView_3" />
            </LinearLayout>

            <LinearLayout android:id="@+id/tab4"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">
                <TextView android:id="@+id/view4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/textView_4" />
            </LinearLayout>
        </FrameLayout>
    </RelativeLayout>
</TabHost>

Java 程式碼:zhnews.java

這裡改用 Activity 加 findViewById 的方式取得 TabHost,記得先呼叫 setup() 再新增分頁,否則會拿到未初始化的物件。

package net.zhnews.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;

public class zhnews extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        setTitle("珠海新聞網Android客戶端");

        TabHost tabs = (TabHost) findViewById(R.id.tabhost);
        tabs.setup();

        TabHost.TabSpec spec = tabs.newTabSpec("tab1");
        spec.setContent(R.id.tab1);
        spec.setIndicator("新聞");
        tabs.addTab(spec);

        spec = tabs.newTabSpec("tab2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("搜聯社");
        tabs.addTab(spec);

        spec = tabs.newTabSpec("tab3");
        spec.setContent(R.id.tab3);
        spec.setIndicator("影像");
        tabs.addTab(spec);

        spec = tabs.newTabSpec("tab4");
        spec.setContent(R.id.tab4);
        spec.setIndicator("設置");
        tabs.addTab(spec);

        tabs.setCurrentTab(0);
    }
}

還可以再優化

這個寫法勝在簡單,但標籤只有純文字,視覺上略顯單薄。若想更接近本文開頭提到的那些電商 App,可以改用 setIndicator() 的雙參數版本搭配 selector 圖示,或用 TabLayout 加上自訂的 tab view,讓選取與未選取狀態有不同的顏色與圖形。

以上就是把 Tab 選單放到螢幕下方的簡便做法,希望對你的 Android 開發有所幫助。

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