編輯:關於 Android 程式設計
新浪微博、京東、淘寶這類 App,都把選項卡放在螢幕最下方當作主框架,但 Android 的 TabWidget 預設是長在上方的。網路上做法很多,這裡分享一個改動最少、最容易理解的版本。
把 TabWidget 放進 RelativeLayout,然後加上 android:layout_alignParentBottom="true",標籤列就會乖乖貼齊螢幕底部,剩下的 FrameLayout 自動佔滿上方空間。
<?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>
這裡改用 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 開發有所幫助。
用 SharedPreferences 保存零散資料:Android 儲存學習筆記
前面幾篇我們一直用純文字檔來存使用者資訊,這種做法有兩個明顯毛病:格式全靠自訂(例如自己拿 ## 當分隔符),既容易出錯也不好維護;再者檔案內容缺乏結構,日後要擴充欄位幾
Android 登入模組實作:SQLite 與 Activity 串接
登入模組是許多應用的基礎功能。以下以簡單範例說明如何在 Android 中使用 Activity、SQLite 與控制項完成帳號驗證。 建立登入畫面 先建立 LoginA
Android 簡訊發送器的實作教學
實作概念 本範例模擬一個簡易的 Android 簡訊發送器。使用者輸入手機號碼與簡訊內容後,點擊按鈕即可透過 SmsManager 送出簡訊。 新增簡訊權限 在 Andr
ViewPager 擴展詳解:打造帶導航的 ViewPagerIndicator(含圖片快取與非同步載入)
用過 ViewPager 的人都知道,它在「橫向切換頁面」這件事上表現很好,但導航樣式的部分預設值相當陽春。GitHub 上有個很不錯的開源專案 ViewPagerInd