底部固定三個 Button,寬度均分螢幕 1/3,無間隙,類似 Tab 效果。
layout_weight + weightSum<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="首頁"
android:background="@null"
android:padding="16dp"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="發現"
android:background="@null"
android:padding="16dp"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="我的"
android:background="@null"
android:padding="16dp"/>
</LinearLayout>
| 屬性 | 值 | 作用 |
|---|---|---|
layout_width | 0dp | 寬度由 weight 決定,忽略 wrap_content |
layout_weight | 1 | 三個各佔 1 份,共 3 份 → 各 1/3 |
weightSum | 3 | 父佈局聲明總份數,避免子 View 計算誤差 |
background | @null | 移除預設灰色背景與邊框線 |
layout_gravity | bottom | 整組靠底(若在 CoordinatorLayout/FrameLayout 中) |
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:menu="@menu/bottom_nav_menu"
app:labelVisibilityMode="labeled"
app:itemBackground="@color/white"/>
優點:內建選中狀態、圖標+文字、Badge、動畫、無障礙支援,符合 Material Design 規範。
新手首選 LinearLayout + weightSum=3, weight=1, width=0dp,簡單可控;項目正式上線建議遷移至 BottomNavigationView,獲得完整的交互體驗與無障礙支援。