导航视图(NavigationView)通常与抽屉布局(DrawerLayout)结合使用,实现了良好的侧滑交互体验。从常规开发来说,侧滑一般都是左侧实现侧滑,抽屉布局可有三个子布局。(注意:第一个子布局一定要是主界面,其次的两个子布局就是左右两侧的布局,左右两侧只放一个也可)
首先在build.gradle中添加引用
implementation 'com.android.support:design:26.1.0'
下面是界面xml文件:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" 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" tools:openDrawer="start"> <include layout="@layout/activity_main2" android:layout_width="match_parent" android:layout_height="match_parent"/> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="left" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_drawer_layout_one" /> </android.support.v4.widget.DrawerLayout>
其中include 位主界面,NavigationView位侧滑菜单,header 为头部文件,当然还可以添加menu(常规侧滑菜单)。
取消侧滑菜单的scrollbars :
/** * 隐藏侧滑菜单的bar */ private void disableNavigationViewScrollbars(NavigationView navigationView) { if (navigationView != null) { NavigationMenuView navigationMenuView = (NavigationMenuView) navigationView.getChildAt(0); if (navigationMenuView != null) { navigationMenuView.setVerticalScrollBarEnabled(false); } } }
当然也有侧滑菜单的开启与关闭事件:
drawer.openDrawer(Gravity.LEFT);
drawer.closeDrawer(Gravity.LEFT);
获取头部的控件:
View headView = navigationView.getHeaderView(0); mMenuIcon = headView.findViewById(R.id.menu_icon);结尾我想说的是我一直不想用menu,用头部文件填写全部的布局,但是在模拟器上却没有完全填充屏幕,真机测试暂时还没有发现问题,尴尬的是我查询api文档最高版本是19没有查询到源码,各位有什么方法,跪求