如何设置NavigationView使其打开时不被BottomNavigation遮挡?
你完全不需要自定义NavigationView来解决这个问题,核心问题出在你的布局层级结构上。当前你的AHBottomNavigation是和DrawerLayout同级放在外层的CoordinatorLayout里,这就导致BottomNavigation的层级比DrawerLayout内部的NavigationView更高,所以抽屉打开时会被遮挡。
修复方案:调整布局层级
把AHBottomNavigation移动到DrawerLayout内部的主内容区域(也就是和NestedScrollView同级的位置),这样DrawerLayout的侧边栏NavigationView就会自然显示在所有主内容控件的上层。
修改后的activity_main.xml代码如下:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:layoutDirection="rtl" tools:openDrawer="start"> <!-- 主内容区域 --> <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" /> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_marginTop="?attr/actionBarSize" android:layout_marginBottom="?attr/actionBarSize" android:layout_height="wrap_content" app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"> <FrameLayout android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="wrap_content"/> </android.support.v4.widget.NestedScrollView> <!-- 把BottomNavigation移到这里,和主内容同级 --> <com.aurelhubert.ahbottomnavigation.AHBottomNavigation android:id="@+id/bottomNavigation_main" android:layout_gravity="bottom" android:layout_alignParentBottom="true" android:layoutDirection="rtl" android:background="@color/colorPrimary" android:fitsSystemWindows="false" android:layout_width="match_parent" app:itemIconTint="@color/navigation_item_color" app:itemTextColor="@color/navigation_item_color" android:layout_height="wrap_content" /> </android.support.design.widget.CoordinatorLayout> <!-- NavigationView 现在会显示在所有主内容控件的上层 --> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="260dp" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" android:background="@color/white" app:itemIconTint="@color/darkGray" app:headerLayout="@layout/layout_navigation_header" app:itemTextColor="@color/darkGray"/> </android.support.v4.widget.DrawerLayout> </android.support.design.widget.CoordinatorLayout>
补充说明
- 抽屉布局
DrawerLayout的规则是:第一个子View是主内容区域,第二个及以后的子View是侧边栏(NavigationView),侧边栏的层级默认高于主内容区域,所以只要把BottomNavigation放到主内容区域里,侧边栏打开时就会覆盖它。 - 如果因为某些原因不能移动布局,也可以尝试给
NavigationView设置更高的elevation属性(比如android:elevation="16dp"),同时降低BottomNavigation的elevation,不过布局层级调整是更稳妥的方案。
你的MainActivity代码不需要做任何修改,保持原样即可。
内容的提问来源于stack exchange,提问作者aliayn
相关产品推荐
相关产品推荐

