編輯:關於 Android 程式設計
TabHost 是實作選項卡界面的經典元件,預設情况下選項卡會顯示在上方。但有時候出於設計考量,我們會希望把它放到螢幕底端。本文分享一個簡單的做法,透過版面結構的安排就能實現。
關鍵在於把 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 之後(也就是下方)。兩者搭配,選項卡自然穩穩落在螢幕底部。這種寫法簡單直觀,很適合需要底部導航風格的介面。
Android 界面編程探險(二):AdapterView、進度條、菜單與對話框
一、UI 組件之 AdapterView 及其子類(二) 1. 自動完成文本框 AutoCompleteTextView MultiAutoCompleteTextVie
Android Fragment 界面向下跳轉與逐層返回的實作方法詳解
本文示範如何在 Android 中使用 Fragment 實現介面向下一層層跳轉、並能一級級返回的效果,供大家參考。核心思路是透過自訂的返回處理介面與抽象 Fragmen
破解 Android 設定中的隱藏彩蛋:隱藏項實現原理解析
做程式的人偶爾也會惡搞一下。Android 的設定裡就藏了這樣一個趣味彩蛋:連續快速點擊「Android 版本」,會冒出一張圖片;而在 4.0 系統上按住 Android
Android 對話框使用大全:AlertDialog、ProgressDialog 與選擇器
對話框類型 Android 常見對話框包括 AlertDialog、ProgressDialog、DatePickerDialog 與 TimePickerDialog。