Android BottomNavigationView底部多余空白如何解决
问题描述
BottomNavigationView显示异常,下方出现多余空白区域,多日排查仍未解决,问题效果如下图所示:
当前使用CoordinatorLayout作为根布局,怀疑空白问题由CoordinatorLayout导致,也有可能和内嵌的Fragment有关,需要移除截图中BottomNavigationView下方的多余空白。
相关代码
布局XML代码
<?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" android:fitsSystemWindows="true" tools:context=".Home_activity"> <!-- 813dp --> <fragment android:id="@+id/fragment" android:name="com.anonymous.nr_chemistryteam.BlankFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.google.android.material.bottomappbar.BottomAppBar android:id="@+id/bottomAppBar" style="@style/Widget.MaterialComponents.BottomAppBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:backgroundTint="@color/white" android:theme="@style/AppThemeMaterial2" app:fabCradleMargin="8dp" app:fabCradleRoundedCornerRadius="50dp" app:fabCradleVerticalOffset="6dp"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/homeNvBottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:background="@drawable/empty_background" app:menu="@menu/bottom_menu" /> </com.google.android.material.bottomappbar.BottomAppBar> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/floatingActionButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="#ff5e00" android:src="@drawable/ic_cart" app:fabCustomSize="72dp" app:layout_anchor="@id/bottomAppBar" app:maxImageSize="34dp" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
菜单XML代码
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/home_page2" android:icon="@drawable/ic_home2" android:title="Home" /> <item android:id="@+id/message_page2" android:icon="@drawable/ic_message1" android:title="Message" /> <item android:id="@+id/placeholder" android:checkable="false" android:title="" /> <item android:id="@+id/help_page2" android:icon="@drawable/ic_help" android:title="Help" /> <item android:id="@+id/settings_page2" android:icon="@drawable/ic_settings" android:title="Settings" /> </menu>
解决方案
该空白是系统窗口边距(WindowInsets)自动适配逻辑、BottomAppBar默认内边距共同导致的,按以下步骤修改即可修复:
- 删除根CoordinatorLayout上的
android:fitsSystemWindows="true"属性,避免根布局全局为系统导航栏预留底部空间。 - 给BottomNavigationView添加属性,关闭系统窗口底部边距的自动适配:
app:paddingBottomSystemWindowInsets="0dp"
- 给BottomAppBar添加属性,清零内置的底部内容内边距:
app:contentPaddingBottom="0dp"
- 针对Android 10+手势导航场景,给BottomAppBar补充以下属性,关闭手势导航条的边距预留:
android:fitsSystemWindows="false" app:layout_insetEdge="none"
如果完成以上修改后仍有极细的空白残留,直接给BottomNavigationView添加负的底部外边距即可,数值根据实际显示效果调整,通常4dp-8dp就足够,参考写法:android:layout_marginBottom="-4dp"
内容的提问来源于stack exchange,提问作者The Unknown
相关产品推荐
相关产品推荐

