用来实现Android侧滑菜单。
1-首先添加依赖:
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:design:23.3.0'
2-在DrawerLayout布局中添加NavigationView。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /></android.support.v4.widget.DrawerLayout>

有几个需要注意的地方:
android:layout_gravity="start"
这个属性需要设置成start,表示从左边弹出。如果设置成end则表示从右边弹出。
设置头部
app:headerLayout="@layout/nav_header_main"

通过menu设置菜单列表:

3-设置菜单按钮的点击事件

4-获取HeadView的控件

5-隐藏某个菜单项

6-添加分割线
menu文件中设置成多个组,并为每个组设置id

7-设置菜单列表中的图标显示原始的颜色
navigationView.setItemIconTintList(null);
8-打开、关闭菜单


9-设置Drawer不能滑动打开,但是可以滑动关闭
首先设置了这个之后不能滑动打开也不能滑动关闭

设置成可以滑动关闭

这样就完成了。
网友评论