求助:BottomNavigationView始终固定在布局顶部无法正常显示
嘿,我帮你定位到问题啦!你的BottomNavigationView一直卡在顶部,主要是两个布局属性设置错了,咱们快速修正:
问题根源
- CoordinatorLayout高度不足:你把CoordinatorLayout的
android:layout_height设为wrap_content,这会让它只包裹内部元素的高度,没办法撑满整个屏幕,BottomNavigationView自然找不到底部的定位点。 - 布局重力设置错误:给BottomNavigationView设置的
android:layout_gravity="start"会让它默认靠左上角对齐,完全没触发底部定位的逻辑。
修改后的布局代码
<?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:orientation="vertical" tools:context=".MainActivity" android:id="@+id/mainLayout"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNavigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom|start" app:menu="@menu/bottom_bar_menu" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
额外提示
如果之后要在布局里添加主内容区域(比如Fragment容器),记得给主内容容器设置android:layout_height="match_parent",并且可以通过app:layout_behavior="@string/appbar_scrolling_view_behavior"让它自动避开BottomNavigationView,避免内容被遮挡。
内容的提问来源于stack exchange,提问作者Herman Hansson Söderlund
相关产品推荐
相关产品推荐

