You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin Bottom Navigation Bar无法对齐屏幕底部问题求助

问题:BottomNavigationView运行时无法对齐屏幕底部

开发带底部导航栏的应用,布局预览显示正常,但运行时导航栏向上偏移,更换不同尺寸虚拟设备结果一致。

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> 
<?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:设置边缘到边缘主题

  1. 在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>
  1. 在Activity的onCreate方法中添加代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Window window = getWindow();
    window.setDecorFitsSystemWindows(false);
}

内容的提问来源于stack exchange,提问作者Eddie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 17:34:50