NavigationView是Android 5.0新特性——Material Design中的一个布局控件,可以结合DrawerLayout使用,让侧滑菜单变得更加美观(可以添加头部布局)。

  NavigationView需要嵌套在DrawerLayout内部,其相对于单独使用DrawerLayout的优点在于可以额外添加一个HeaderView头部布局。另外,NavigationView中的其他选项都是用menu的形式来编写的,menu中的分支也可以在NavigationView中形成分栏效果。

  和其他MD控件一样,使用NavigationView需要在gradle文件中注册依赖:

compile 'com.android.support:design:24.1.1'

1、NavigationView的属性:

            app:headerLayout:NavigationView的头部布局,其中可以存放ImageView、TextView等控件
app:menu:NavigationView中的Item项,存在menu中

2、布局示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:orientation="vertical"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize" /> <android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!-- 主界面布局 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DDDDDD" /> <android.support.design.widget.NavigationView
android:id="@+id/navigationview"
android:layout_width="256.0dip"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
app:headerLayout="@layout/sideworks_navhead"
app:menu="@menu/navigate" /> </android.support.v4.widget.DrawerLayout>
</LinearLayout>

  menu文件中的代码如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group>
<item
android:id="@+id/one_one"
android:icon="@mipmap/ic_launcher"
android:title="Group one Item one" />
<item
android:id="@+id/one_two"
android:icon="@mipmap/ic_launcher"
android:title="Group one Item two" />
<item
android:id="@+id/one_three"
android:icon="@mipmap/ic_launcher"
android:title="Group one Item three" />
</group>
<item android:title="Group Two">
<menu>
<item
android:id="@+id/two_one"
android:icon="@mipmap/ic_launcher"
android:title="Group one Item one" />
<item
android:id="@+id/two_two"
android:icon="@mipmap/ic_launcher"
android:title="Group one Item two" />
</menu>
</item>
</menu>

  头部布局中的代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="center_horizontal"
android:orientation="vertical"> <ImageView
android:id="@+id/header_image"
android:layout_width="150.0dip"
android:layout_height="150.0dip"
android:layout_marginTop="20.0dip"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_launcher" /> <TextView
android:id="@+id/header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20.0dip"
android:layout_marginTop="10.0dip"
android:text="Navigation View"
android:textColor="#FFFFFFFF"
android:textSize="18.0sp"
android:textStyle="bold" /> </LinearLayout>

3、弹出抽屉:

  在JAVA代码中通过点击Toolbar中的ActionBarDrawerToggle来弹出抽屉。代码如下:

        // 绑定Toolbar到Activity中
setSupportActionBar(toolbar);
// 在Toolbar上设置一个按钮,点击这个按钮可以拉出抽屉
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(MainActivity.this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
toggle.syncState();
drawer.addDrawerListener(toggle);

  最终的演示效果如下图:

  以上就是对NavigationView的简单用法的介绍,下面贴出码云上的源码,供大家参考。

DEMO地址

【Android - 控件】之MD - NavigationView的使用的更多相关文章

  1. RxJava RxBinding RxView 控件事件 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. [Android Pro] android控件ListView顶部或者底部也显示分割线

    reference to  :  http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...

  3. Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像

    此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...

  4. Android 控件架构及View、ViewGroup的测量

    附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...

  5. Android - 控件android:ems属性

    Android - 控件android:ems属性http://blog.csdn.net/caroline_wendy/article/details/41684255?utm_source=tui ...

  6. Android 控件知识点,

    一.Android控件具有visibility属性,可以取三个值:visible(默认值)可见,invisible(不可见,但仍然占据原有的位置和大小,可以看做是变得透明了),gone(空间不仅不可见 ...

  7. UIAutomator定位Android控件的方法

    UIAutomator各种控件定位的方法. 1. 背景 使用SDK自带的NotePad应用,尝试去获得在NotesList那个Activity里的Menu Options上面的那个Add note菜单 ...

  8. 从Android系统出发,分析Android控件构架

    从Android系统出发,分析Android控件构架 Android中所有的控件追溯到根源,就是View 和ViewGroup,相信这个大家都知道,但是大家也许会不太清楚它们之间的具体关系是什么,在A ...

  9. Android控件系列之RadioButton&RadioGroup(转)

    学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...

  10. 第三个 android控件

    android控件以及控件对应的属性:

随机推荐

  1. C语言存储类别和链接

    目录 C语言存储类别和链接 存储类别 存储期 五种存储类别 C语言存储类别和链接 ​ 最近详细的复习C语言,看到存储类别的时候总感觉一些概念模糊不清,现在认真的梳理一下.C语言的优势之一能够让程序员恰 ...

  2. VSCode实现文献管理

    1 常用文献管理软件 常用的文献管理软件有mendely,zotero,endnote和Papers(需要付费),具体对比参考链接1.1.1.2 笔者只用过Mendely,当时综合考虑挑了Endnot ...

  3. 第二十五章 system v消息队列(一)

    IPC对象的持续性 随进程持续 :一直存在直到打开的最后一个进程结束.(如pipe和FIFO) 随内核持续 :一直存在直到内核自举(内核自举就是把主引导记录加载到内存,并跳转执行这段内存)或显示删除( ...

  4. 近期学习es6后对变量提升及let和const的一点思考

    1.变量提升:(创建->初始化)-->赋值-->修改 就是说,以var声明的变量,它的声明会被提升到当前作用域的顶端(注意是变量声明提升,变量的赋值没有提升) //在if语句中也会提 ...

  5. FastDFS图片服务器单机安装步骤

    前面已经讲 一张图秒懂微服务的网络架构,通过此文章可以了解FastDFS组件中单机安装流程. 单机版架构图 以下为单机FastDFS安装步骤 一.环境准备 CentOS 7.X libfastcomm ...

  6. python入门三元运算

    三元运算又称三目运算,是对简单条件语句的简写: a = 1 b = 2 c = ' ' c = a  if  a > b  else b 在上面中首先判断a是否大于b如果为真则c = a,如果为 ...

  7. python协程总结

    概述 python多线程中因为有GIL(Global Interpreter Lock 全局解释器锁 )的存在,所以对CPU密集型程序显得很鸡肋:但对IO密集型的程序,GIL会在调用IO操作前释放,所 ...

  8. php判断是不是https的方法

    php判断是不是https的方法<pre> public function is_https() { if (!empty($_SERVER['HTTPS']) && st ...

  9. Eclipse中修改tomcat的部署路径deploypath

    在eclipse上面部署web项目后,它没有将你的项目文件放到tomcat 的目录下面.而是放在了你的工作目录下面. 在tomcat上右键选择“stop” .停止eclipse内的Tomcat服务器 ...

  10. [UWP]通过自定义XamlCompositionBrushBase实现图片平铺

    1. 什么是XamlCompositionBrushBase 我早就想试试自定义XamlCompositionBrushBase,但一直没机会.上一篇文章介绍到使用Win2D的BorderEffect ...