效果图

添加依赖库
(要是高版本的Android Studio,则已经为我们自动添加了所需的依赖库)

dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])compile 'com.android.support:appcompat-v7:22.2.1'compile 'com.android.support:design:22.2.1'
}

创建菜单项
nav_menu.xml

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"><group android:checkableBehavior="single"><item
            android:id="@+id/home"android:title="Home"android:icon="@drawable/ic_home"/><item
            android:id="@+id/settings"android:title="Settings"android:icon="@drawable/ic_setting"/><item
            android:id="@+id/trash"android:title="Trash"android:icon="@drawable/ic_trash"/><item
            android:id="@+id/logout"android:title="Logout"android:icon="@drawable/ic_exit"/></group>
</menu>

创建布局
nav_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="190dp"android:paddingTop="20dp"android:paddingBottom="20dp"android:background="@color/colorPrimary"android:orientation="vertical"><ImageView
        android:id="@+id/image_view"android:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="1"android:src="@drawable/ic_person"android:layout_gravity="center"/><TextView
        android:id="@+id/tv_email"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1234563278@126.com"android:layout_gravity="center"android:textSize="20sp"android:textColor="@color/White"/></LinearLayout>

nav_menu与nav_header分别为NavigationView中的头部与菜单项。记得NavigationView必须以DrawerLayout为父容器

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextView
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Navigation Drawer"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:textSize="25sp"/></RelativeLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.design.widget.CoordinatorLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><android.support.v7.widget.Toolbarandroid:id="@+id/tool_bar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="@color/colorPrimary"></android.support.v7.widget.Toolbar></android.support.design.widget.CoordinatorLayout><android.support.design.widget.NavigationViewandroid:id="@+id/navigation_view"android:layout_width="wrap_content"android:layout_height="match_parent"app:headerLayout="@layout/nav_header"app:menu="@menu/nav_menu"android:layout_gravity="start"></android.support.design.widget.NavigationView></android.support.v4.widget.DrawerLayout>

界面

package com.example.demo.navigationviewtest;import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {private DrawerLayout mDrawerLayout;private ActionBarDrawerToggle mActionBarDrawerToggle;private Toolbar mToolbar;private NavigationView mNavigationView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);mToolbar = (Toolbar) findViewById(R.id.tool_bar);mNavigationView = (NavigationView) findViewById(R.id.navigation_view);mToolbar.setTitle("NavigationView");setSupportActionBar(mToolbar);mActionBarDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,mToolbar,R.string.open,R.string.close){@Overridepublic void onDrawerOpened(View drawerView) {super.onDrawerOpened(drawerView);}@Overridepublic void onDrawerClosed(View drawerView) {super.onDrawerClosed(drawerView);}};mActionBarDrawerToggle.syncState();mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {@Overridepublic boolean onNavigationItemSelected(MenuItem menuItem) {int id = menuItem.getItemId();switch (id) {case R.id.home:Toast.makeText(getApplicationContext(), "Home", Toast.LENGTH_SHORT).show();mDrawerLayout.closeDrawers();break;case R.id.settings:Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show();mDrawerLayout.closeDrawers();break;case R.id.trash:Toast.makeText(getApplicationContext(), "Trash", Toast.LENGTH_SHORT).show();mDrawerLayout.closeDrawers();break;case R.id.logout:Toast.makeText(getApplicationContext(), "Logout", Toast.LENGTH_SHORT).show();mDrawerLayout.closeDrawers();break;}return true;}});TextView tv_email = (TextView)findViewById(R.id.tv_email);tv_email.setText("http://blog.csdn.net/wiseclown");}}

注意
compile 'com.android.support:design:23.2.0',则可以使用addDrawerListener来替代setDrawerListener,以及NavigationView中添加了getHeaderView( )方法来获取Header view。

源码地址:https://github.com/xkck/NavigationViewTest

参考文章
[1]http://blog.csdn.net/lmj623565791/article/details/46405409
[3]http://www.jianshu.com/p/76e30f87a4ed
[2]http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0608/3011.html

Android Design新特性-NavigationView实现抽屉式相关推荐

  1. 魅族android p,2.Android P新特性实践-Slices

    Android P新特性实践-Slices 概述 在2018年谷歌I/O开发者大会上,谷歌发布了万众期待的Android最新版本-Android P.Slice作为Android P版本更新的一大功能 ...

  2. Android N新特性

    Android N新特性 Jit编译器,安装更快,apk占用空间更小 Vulkan渲染引擎(用于游戏开发) Doze模式更加智能 通知栏(同类通知合并.快捷回复) 分屏模式及快捷切换 无缝更新

  3. android 通知历史,Android P新特性:追踪应用通知历史

    原标题:Android P新特性:追踪应用通知历史 IT之家3月9日消息 不久前,谷歌已经正式推出了首个Android P开发者预览版,包含了许多新特性.对此,IT之家也进行了一系列报道.该系统的新特 ...

  4. Android O 新特性和行为变更总结

    Android O新特性和行为变更总结 原文地址:https://mp.weixin.qq.com/s?__biz=MzI1NjEwMTM4OA==&mid=2651232573&id ...

  5. Android O新特性和行为变更总结zz

    https://mp.weixin.qq.com/s/Ezfm-Xaz3fzsaSm0TU5LMw Android O 行为变更 https://developer.android.google.cn ...

  6. 社区说|Android 13 新特性 EROFS-只读文件系统解析

    活动时间 7 月 28 日(本周四) 20:00 - 21:00 活动日程 20:00 - 20:45 主题分享 Android 13新特性 EROFS-只读文件系统解析 介绍 Android 13的 ...

  7. Android O新特性和行为变更总结

    原文地址(QQ音乐微信公众号首发): https://mp.weixin.qq.com/s?__biz=MzI1NjEwMTM4OA==&mid=2651232573&idx=1&am ...

  8. Android N 新特性

    2016年5月19日,谷歌在美国加州的山景城举办了 Google I/O 开发者大会中发布.2016年6月,Android N正式命名为"牛轧糖" 本届I/O开发者大会上,Goog ...

  9. 魅族新系统android o,Android O新特性 原来魅族Flyme6早已实现

    原标题:Android O新特性 原来魅族Flyme6早已实现 在Android N系统发布一年之后,谷歌3月22日又推出了新一代的 Android O 开发者预览版.在Android O的开发者预览 ...

最新文章

  1. 大脑简史(2)-研究大脑的手段
  2. 夏日里的激情——FE鹅和鸭农庄行
  3. 基于机器学习的web异常检测(转)
  4. admui3字体无法删除_被微信好友删除、拉黑还不知?这3个方法可以自查,看完涨知识了...
  5. const成员或者引用成员必须使用构造函数初始化列表的方式
  6. 【转】Luajit-2.1.0-beta1的发布和生成arm64用bytecode的解脱
  7. SharePoint Server 2010 一步一步从入门到精通
  8. Wtm Blazor来了!
  9. 《鸿蒙理论知识05》HarmonyOS概述之下载与安装软件
  10. oschina git服务, 如何生成并部署ssh key
  11. ExtJS的extend(Ext Designer的使用)
  12. LeetCode 1115. 交替打印FooBar
  13. jQuery Mobile中单选按钮radio的data-*选项
  14. uniapp 如何给搜索框设值_uni-app 顶部配置搜索框和左右图标
  15. U盘的针脚板竟然掉了
  16. android js 弹窗,Android原生代码拦截H5 Web页面中JavaScript弹窗/弹框
  17. 计算机英语 复习资料
  18. opc服务器变量为空,OPC客户机上浏览不到OPC服务器上的变量
  19. 使用python打印九九乘法表
  20. android获取imei需要read_phone_state吗,Firemonkey android read_phone_state运行时权限要求获取IMEI...

热门文章

  1. 服务器监控系统的介绍,客户服务系统服务器监控系统
  2. input placeholder的样式
  3. 《Python核心编程 》笔记-第二章+第三章
  4. 统计学习笔记(1)——统计学习方法概论
  5. Steve Lin:如何撰写一篇优秀的SIGGRAPH论文
  6. Oracle编程入门经典 第1章 了解Oracle
  7. 较高人工智能的人机博弈程序实现(多个算法结合)含C++源码
  8. 多种特征提取算法比较汇总
  9. Onew——全球领先的区块链数字资产服务商
  10. linux每日命令(14):less命令