傳統 TabHost 預設標籤在頂部,若需置底(類似 iOS TabBar / Material BottomNavigationView),需調整佈局層級與權重分配。
<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:id="@+id/ll_main_tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 內容區:FrameLayout 佔滿剩餘空間 -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fadingEdge="none">
<RelativeLayout android:id="@+id/tabFirst" ...>
<AnalogClock android:layout_centerInParent="true"/>
</RelativeLayout>
<RelativeLayout android:id="@+id/tabSecond" ...>
<DigitalClock android:layout_centerInParent="true"/>
</RelativeLayout>
</FrameLayout>
<!-- 標籤欄:置底 -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/tab_btn_bg_normal" />
</LinearLayout>
</TabHost>
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("one").setIndicator("aaaa").setContent(R.id.tabFirst));
tabHost.addTab(tabHost.newTabSpec("two").setIndicator("bbb").setContent(R.id.tabSecond));
tabHost.addTab(tabHost.newTabSpec("three").setIndicator("ccc")
.setContent(new Intent(this, LogActivity.class)));
tabHost.setCurrentTab(0);
}
}
TabHost 為根佈局,內含垂直 LinearLayout;FrameLayout (tabcontent) 高度 0dp + weight=1 佔據所有剩餘空間;TabWidget (tabs) 置於 LinearLayout 末尾,自然顯示在底部;setContent() 支援 View ID 或 Intent 兩種方式指定分頁內容。新專案建議使用 BottomNavigationView (Material Design) + ViewPager2 + Fragment,提供更豐富動畫、Badge、移位模式等體驗,TabHost 已棄用維護模式。