首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android 虛擬鍵盤遮擋底部導航欄:百分比佈局 + weight 權重適配方案

     編輯:程式設計綜合問答

使用百分比佈局(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" 頂住導航欄上方
  • 百分比/權重僅用於內容區內部子元素分配,不直接作用於根容器高度

驗證

開啟「開發者選項 → 模擬帶虛擬按鍵的螢幕」或真機測試,底部導航不再被遮擋。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved