求助:BottomNavigationView打开时覆盖整个Activity的解决方法
我来帮你搞定这个问题!你遇到的空白覆盖主布局的问题,核心是当前布局里只放了BottomNavigationView,缺少主内容区域的布局约束,导致ConstraintLayout的空白区域挤占了主布局空间;而直接移除ConstraintLayout后,因为没有布局容器来控制导航栏的位置,它就默认跑到顶部了。
下面是调整后的布局方案,能让BottomNavigationView悬浮在主布局上方,同时固定在屏幕底部:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BottomNav"> <!-- 主内容容器,可替换为你实际的主布局,比如LinearLayout/RelativeLayout,或用来承载Fragment的FrameLayout --> <FrameLayout android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@id/navigation" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/> <!-- 底部导航栏 --> <android.support.design.widget.BottomNavigationView android:id="@+id/navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/dir_primary" android:elevation="4dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_nav" /> </android.support.constraint.ConstraintLayout>
关键调整说明:
- 新增了主内容容器:这里用
FrameLayout作为示例,你可以直接替换成自己的主布局内容,或者用它来加载Fragment页面。 - 主内容容器的高度设为
0dp:通过constraintTop和constraintBottom属性,让它自动填充导航栏上方的所有可用空间,不会被导航栏覆盖。 - 导航栏保留底部约束:同时因为主内容布局在它上方,它会自然悬浮在主内容之上,不会出现多余的空白区域。
这样调整后,打开BottomNav Activity时,底部导航栏就会固定在屏幕底部,悬浮在主内容布局上方,完美解决你的问题!
内容的提问来源于stack exchange,提问作者Joel Pou
相关产品推荐
相关产品推荐

