BottomNavigation遮挡DrawerLayout中FrameLayout内容的问题求助
问题根源
你的FrameLayout设置了match_parent高度,而BottomNavigationView覆盖在它上方,导致Fragment中的底部内容被遮挡。
修复方案(基于原RelativeLayout布局)
只需给FrameLayout添加底部约束,让它的底部停在BottomNavigationView上方即可:
- 给
FrameLayout添加android:layout_above="@id/bottom_navigation_ID"属性 - 移除
FrameLayout上无效的app:layout_behavior="@string/appbar_scrolling_view_behavior"(该行为仅配合AppBarLayout使用,当前布局无AppBarLayout,属性无意义) - 移除
BottomNavigationView上的android:layout_gravity="bottom|end"(RelativeLayout布局下该属性无效)
修改后的核心布局代码:
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/bottom_navigation_ID" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation_ID" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="?android:attr/windowBackground" app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" android:foreground="?attr/selectableItemBackground" app:itemIconTint="@color/bottom_navigation_color" app:labelVisibilityMode="unlabeled" app:menu="@menu/navigation"/> </RelativeLayout>
备选方案:改用ConstraintLayout布局
如果更倾向于ConstraintLayout,替换外层RelativeLayout为ConstraintLayout后,设置FrameLayout的约束:
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toTopOf="@id/bottom_navigation_ID" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation_ID" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" android:background="?android:attr/windowBackground" app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" android:foreground="?attr/selectableItemBackground" app:itemIconTint="@color/bottom_navigation_color" app:labelVisibilityMode="unlabeled" app:menu="@menu/navigation"/> </androidx.constraintlayout.widget.ConstraintLayout>
设置后,FrameLayout会自动填充底部导航上方的所有空间,Fragment内容就不会被遮挡了。
内容的提问来源于stack exchange,提问作者developer
相关产品推荐
相关产品推荐

