編輯:進階開發
系統內建的 TabActivity 外觀相當樸素,實作專案時往往需要客製。常見需求有兩類:一是改變 Tab 欄的位置,例如移到畫面下方;二是替換 Tab 的圖示與背景樣式。兩者都能透過「覆寫系統佈局」的方式完成。
做法是在 TabActivity 的 onCreate 中指定自訂的內容佈局:
setContentView(R.layout.tab_host);
其中 tab_host.xml 是從系統資源中取出後略作修改而來。系統原始版本大致如下:
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="0" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="match_parent" android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
關鍵在於 TabWidget 與 FrameLayout 的順序,以及那兩個 layout_weight。只要把兩者調換位置,Tab 欄自然就落到畫面下方:
<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:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1" />
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="0" />
</LinearLayout>
</TabHost>
最容易被忽略的一點:三個 View 的 id 千萬不能改。 TabHost、TabWidget、tabcontent 這三個 id 是 TabActivity 內部查找控制項的依據,改了會直接導致執行期崩潰。FrameLayout 用 0dip 高度搭配 weight=1,是為了讓它吃掉扣除 Tab 欄之後的所有剩餘空間。
系統內建的 tab_indicator.xml 結構如下:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
android:layout_weight="1"
android:layout_marginLeft="-3dip"
android:layout_marginRight="-3dip"
android:background="@android:drawable/tab_indicator">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle" />
</RelativeLayout>
從結構可以看出,預設是「圖示在上、文字在下」,且圖示大小受限於 wrap_content,很難鋪滿整個格子。若設計稿要求圖示作為整塊背景、或需要不同於系統的視覺層次,最乾脆的做法是自己寫一份 indicator 佈局,再透過 setIndicator(View) 直接塞入。
先準備一個簡單的容器佈局 tab_in.xml:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="64dip" />
接著在程式中為每個分頁 inflate 一份,各自設定背景(背景使用 selector 就能做出選取/未選取的狀態切換),然後加入 TabHost:
LayoutInflater inflater = LayoutInflater.from(this);
View view1 = inflater.inflate(R.layout.tab_in, null);
View view2 = inflater.inflate(R.layout.tab_in, null);
View view3 = inflater.inflate(R.layout.tab_in, null);
view1.setBackgroundResource(R.drawable.tab_upload_stateful);
view2.setBackgroundResource(R.drawable.tab_download_stateful);
view3.setBackgroundResource(R.drawable.tab_receive_stateful);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(view1).setContent(intent1));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator(view2).setContent(intent2));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator(view3).setContent(intent3));
這樣就完成了一組外觀完全由設計稿掌控的 Tab。
selector 是關鍵。 讓「選取中/未選取」的視覺差異交給 state_selected 的 ColorStateList 或 StateListDrawable 處理,就不需要在程式裡手動切換圖片,程式碼乾淨很多。
TabActivity 已經退場。 這個類別在後續版本被標記為不建議使用。新專案請改用 Fragment 搭配 TabLayout 或 ViewPager2, styling 透過 app:tabBackground、app:tabIndicatorColor 等屬性就能完成,不必再覆寫系統佈局。上面這套「取出系統佈局、改寫、再指定」的思路仍然值得理解,因為它正是 framework 客製化的通用手法。
Android+ARM 組合在 Computex 2009 大放異彩
報告中寫道:「Android 是首款受到強力消費品牌青睞的 Linux 作業系統,而它背後的這隻強力推手,正是 Google。」 報告也坦承,Android 目前仍
探索繽紛的Android應用系統:WiFi不掉線與聯盟陣容
隨著Android應用系統問世,廣大Android愛好者总算放下了心裡的期待。作為Google企業策略的一環,它在手機作業系統領域佔有一席之地幾乎是註定的事。 WiFi
Android 程式迎來大爆發?
由於 Android 平台本身的優勢,引來了眾多手機廠商的關注。 一大堆 Android 手機即將發售,開發人員紛紛向該平台投入巨大資源,這可能迎來 Android 程
Sidekick 的故事:從 Danger 到 Android(4)明星寵兒與轉折的開始
2004 年秋,明星擁抱新 Sidekick 2003 年的彩色版讓 Sidekick 進入了明星的視野,Paris Hilton 與 Lindsay Lohan 都是使