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

Android 如何把選項卡 TabHost 放到螢幕底部

編輯:關於 Android 程式設計

TabHost 是實作選項卡界面的經典元件,預設情况下選項卡會顯示在上方。但有時候出於設計考量,我們會希望把它放到螢幕底端。本文分享一個簡單的做法,透過版面結構的安排就能實現。

核心技巧:調整 LinearLayout 的順序與權重

關鍵在於把 TabWidget 放在 LinearLayout 的最下方,並讓承載內容的 FrameLayout 使用 layout_weight="1" 佔滿剩餘空間。這樣 TabWidget 就會被「推」到螢幕底部,而內容區自動填滿上方。

參考程式碼 如下:

<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:padding="5dp" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</TabHost>

要點提醒

這裡有兩個不能忽略的細節:一是 FrameLayout 的高度設為 0dip 搭配 layout_weight="1",讓它撐滿剩餘空間;二是 TabWidget 必須放在 FrameLayout 之後(也就是下方)。兩者搭配,選項卡自然穩穩落在螢幕底部。這種寫法簡單直觀,很適合需要底部導航風格的介面。

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