Kotlin Bottom Navigation Bar无法对齐屏幕底部问题求助
开发带底部导航栏的应用,布局预览显示正常,但运行时导航栏向上偏移,更换不同尺寸虚拟设备结果一致。
Main Activity 布局代码
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Home"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Home" android:textSize="50sp" android:textStyle="bold" android:layout_centerInParent="true" android:textColor="@color/black" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigator" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_margin="0dp" android:padding="0dp" app:itemBackground="@color/cardview_dark_background" app:itemTextColor="@drawable/selector" app:itemIconTint="@drawable/selector" app:menu="@menu/menu" /> </RelativeLayout>
Menu 代码
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/dashboard" android:title="Dashboard" android:icon="@drawable/ic_dashboard" /> <item android:id="@+id/about" android:title="About" android:icon="@drawable/ic_about" /> <item android:id="@+id/home" android:title="Home" android:icon="@drawable/ic_home" /> </menu>
布局预览效果

应用运行效果

解决方案
核心原因是布局未适配系统导航栏区域,导致RelativeLayout的底部被系统UI挤压,出现偏移。以下是三种可行解决办法:
方法1:用ConstraintLayout替代RelativeLayout(推荐)
ConstraintLayout对现代Android布局适配更友好,能精准控制组件位置:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Home"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Home" android:textSize="50sp" android:textStyle="bold" android:textColor="@color/black" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigator" android:layout_width="match_parent" android:layout_height="wrap_content" app:itemBackground="@color/cardview_dark_background" app:itemTextColor="@drawable/selector" app:itemIconTint="@drawable/selector" app:menu="@menu/menu" app:layout_constraintBottom_toBottomOf="parent" android:fitsSystemWindows="true"/> </androidx.constraintlayout.widget.ConstraintLayout>
方法2:给根布局添加适配属性
在原RelativeLayout根节点添加android:fitsSystemWindows="true",让布局自动适配系统窗口:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".Home">
方法3:设置边缘到边缘主题
- 在
res/values/styles.xml修改主题:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <!-- 保留原有属性,新增以下配置 --> <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item> <item name="android:windowTranslucentNavigation">true</item> </style>
- 在Activity的
onCreate方法中添加代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Window window = getWindow(); window.setDecorFitsSystemWindows(false); }
内容的提问来源于stack exchange,提问作者Eddie
相关产品推荐
相关产品推荐

