美文网首页
DrawerLayout+NavigationView的使用

DrawerLayout+NavigationView的使用

作者: 苏简笔记 | 来源:发表于2016-07-27 15:36 被阅读404次

用来实现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不能滑动打开,但是可以滑动关闭

首先设置了这个之后不能滑动打开也不能滑动关闭


Paste_Image.png

设置成可以滑动关闭


Paste_Image.png

这样就完成了。

相关文章

网友评论

      本文标题:DrawerLayout+NavigationView的使用

      本文链接:https://www.haomeiwen.com/subject/idgvjttx.html