使用百分比佈局(PercentFrameLayout/ConstraintLayout guideline)適配大多數機型,但帶虛擬導航欄(底部 Home/Back/Recent)的手機上,導航欄佔用屏幕高度,導致底部 Tab 被遮擋。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"> <!-- 關鍵屬性 -->
<!-- 內容區 -->
<View android:id="@+id/content"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottom_nav"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<!-- 底部導航 -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
android:fitsSystemWindows="true" 讓根佈局感知系統窗口(狀態欄/導航欄)內邊距layout_constraintBottom_toBottomOf="parent" 錨定父容器底部(含系統邊距)layout_constraintBottom_toTopOf="@id/bottom_nav" 頂住導航欄上方開啟「開發者選項 → 模擬帶虛擬按鍵的螢幕」或真機測試,底部導航不再被遮擋。