編輯:關於 Android 程式設計
Android 早期的 TabActivity 預設把標籤列放在頂端,但多數現代應用的底部導覽列設計更符合拇指操作習慣。要改位置,關鍵在於理解 TabHost 內部由 TabWidget 與 FrameLayout 組成的結構,並用 LinearLayout 的垂直排列重新安排順序。
核心觀念:把 TabWidget 寫在 FrameLayout 之後,這樣在垂直 LinearLayout 中它就會落在下方。FrameLayout 加上 layout_weight="1" 佔滿剩餘空間,標籤列則以 wrap_content 貼齊底部。
<?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:orientation="vertical"
android:id="@+id/ll_main_tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 內容區:佔滿上方剩餘空間 -->
<FrameLayout
android:id="@android:id/tabcontent"
android:gravity="center"
android:fadingEdge="none"
android:fadingEdgeLength="0.0dip"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0">
<RelativeLayout
android:id="@+id/tabFirst"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<AnalogClock
android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/tabSecond"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<DigitalClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</FrameLayout>
<!-- 標籤列:放在內容區之後,就會顯示在下方 -->
<TabWidget
android:id="@android:id/tabs"
android:background="@drawable/tab_btn_bg_normal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
這裡有幾個 id 必須嚴格遵守:@android:id/tabhost、@android:id/tabcontent、@android:id/tabs。這三個是 TabHost 內部以程式碼尋找子元件時使用的固定識別碼,寫錯任何一個都會在執行時拋出空指針例外。這也是自訂 TabHost 版面最常見的失敗原因。
package com.tanghy.tab.activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
// 使用版面中的 View id 作為內容
tabHost.addTab(tabHost.newTabSpec("one")
.setIndicator("aaaa")
.setContent(R.id.tabFirst));
tabHost.addTab(tabHost.newTabSpec("two")
.setIndicator("bbb")
.setContent(R.id.tabSecond));
// 使用 Intent 跳轉到另一個 Activity 作為內容
tabHost.addTab(tabHost.newTabSpec("three")
.setIndicator("ccc")
.setContent(new Intent().setClass(this, LogActivity.class)));
tabHost.setCurrentTab(0);
}
}
從程式碼可以看出,setContent() 支援兩種寫法:傳入版面中的 View id(如上面前兩個),或傳入一個 Intent(如第三個,會啟動對應的 Activity 並把它的畫面嵌進內容區)。後者更模組化,適合每個分頁都是獨立功能頁的情況。
TabActivity 自 API 13 起就被標記為 deprecated,官方建議改用 Fragment 搭配 ViewPager,或直接採用 BottomNavigationView、Material Design 的 TabLayout。新專案請不要再用 TabActivity。setIndicator("aaaa") 只設定了純文字。若要顯示圖示,可改用 setIndicator("文字", Drawable) 的重載版本,或傳入自訂的 View。getTabWidget() 後遍歷子 View 修改 LayoutParams,這部分可參考 TabWidget 樣式調整的相關做法。BottomNavigationView 幾行 XML 就能完成,並自動處理切換動畫、選中狀態與無障礙標籤,程式碼量遠少於上述範例。
Android IPC Binder 深入解析:架構、協議與服務發現流程
Android IPC Binder 深入解析 Binder 是 Android 中非常重要的跨進程通信機制。它原本可以視為一種 IPC 工具,但在 Android 系統
Android 官方指南:全面支持不同屏幕尺寸的實現之道
本文將介紹如何讓你的應用自適應各種屏幕尺寸,主要途徑包括:讓布局充分自適應、按屏幕配置加載合適的 UI、確保正確的布局用在正確的設備上,以及提供能隨屏幕伸縮的圖片。 使用
Android Studio 導入第三方庫的完整流程與常見踩坑
前言多數人踏入 Android 開發時,都會選擇 Google 官方的 Android Studio。由於它與早期的 Eclipse 差異不小,入門時遇到的問題也格外多,
OkHttp3 + Retrofit2 在 Android 專案中的網路請求優化實踐
一、開發背景 手上維護的是一個已經跑了三年的老專案。專案啟動時 OkHttp 還沒有現在這樣的地位,幾乎所有 HTTP 請求都靠 HttpURLConnection 完成