BottomAppBar与BottomNavigation底部出现额外空白的问题排查
在使用BottomAppBar嵌套BottomNavigationView实现底部导航时,页面底部出现了无法消除的额外空白区域。
使用环境
- Android Studio Giraffe版本
- 依赖配置:
implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.6.0'
布局代码
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout 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=".ui.MainActivity"> <!--Nav Host--> <androidx.fragment.app.FragmentContainerView android:id="@+id/navHost" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" /> <!--Bottom menu--> <com.google.android.material.bottomappbar.BottomAppBar android:id="@+id/mainBottomAppbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@color/white" app:fabAnimationMode="scale" app:fabCradleMargin="@dimen/_10mdp" app:fabCradleRoundedCornerRadius="@dimen/_15mdp" app:fabCradleVerticalOffset="@dimen/_10mdp"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/mainBottomNav" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/_15mdp" app:itemIconTint="@color/item_bottom_nav_tint" app:itemTextColor="@color/item_bottom_nav_tint" app:labelVisibilityMode="selected" app:menu="@menu/menu_main" /> </com.google.android.material.bottomappbar.BottomAppBar> <!--Menu--> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/mainFabMenu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/app_name" android:src="@drawable/ic_person_circle_plus" app:backgroundTint="@color/Blue_Lagoon" app:layout_anchor="@id/mainBottomAppbar" app:tint="@color/white" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
原因及解决方案
核心原因
- BottomAppBar默认内边距:Material 1.6.0版本的BottomAppBar会自动添加适配系统导航栏的底部内边距,若设备无底部导航栏或未正确配置系统栏适配,该内边距会转化为额外空白。
- 嵌套布局高度冲突:BottomNavigationView设置为
wrap_content时,加上BottomAppBar的默认内边距,会导致底部整体高度超出预期,和CoordinatorLayout的布局逻辑叠加后出现空白。
解决方案
方案1:强制移除BottomAppBar底部内边距
给BottomAppBar添加android:paddingBottom="0dp",覆盖默认内边距:
<com.google.android.material.bottomappbar.BottomAppBar ... android:paddingBottom="0dp">
方案2:调整BottomNavigationView高度
将BottomNavigationView的android:layout_height改为match_parent,让它填满BottomAppBar的可用空间:
<com.google.android.material.bottomnavigation.BottomNavigationView ... android:layout_height="match_parent" />
方案3:升级Material库版本
1.6.0版本存在布局适配bug,升级至最新稳定版(如1.11.0)可修复此类问题:
implementation 'com.google.android.material:material:1.11.0'
方案4:修正系统栏适配逻辑
在Activity的onCreate方法中添加系统栏适配代码,确保BottomAppBar与系统导航栏正确对齐:
WindowCompat.setDecorFitsSystemWindows(window, false)
内容的提问来源于stack exchange,提问作者Hosein.m.barazandeh
相关产品推荐
相关产品推荐

