需要添加的包:

测试代码:

  1. package com.zzw.navigationview;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.support.design.widget.NavigationView;
  6. import android.support.design.widget.NavigationView.OnNavigationItemSelectedListener;
  7. import android.support.v4.view.GravityCompat;
  8. import android.support.v4.widget.DrawerLayout;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.ImageView;
  13.  
  14. public class MainActivity extends Activity {
  15.  
  16. private DrawerLayout drawerLayout;
  17. private ImageView imageView;
  18. private NavigationView navigationView;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23.  
  24. initView();
  25. // navigationView选中Item处理
  26. handNavigationView();
  27.  
  28. findViewById(R.id.button).setOnClickListener(new OnClickListener() {
  29.  
  30. @Override
  31. public void onClick(View v) {
  32.  
  33. drawerLayout.openDrawer(GravityCompat.START);
  34. }
  35. });
  36. }
  37.  
  38. private void handNavigationView() {
  39. navigationView
  40. .setNavigationItemSelectedListener(new OnNavigationItemSelectedListener() {
  41.  
  42. // 用于辨别此前是否已有选中条目
  43. MenuItem preMenuItem;
  44.  
  45. @Override
  46. public boolean onNavigationItemSelected(MenuItem menuItem) {
  47. // 首先将选中条目变为选中状态
  48. // 即checked为true,后关闭Drawer,以前选中的Item需要变为未选中状态
  49. if (preMenuItem != null)
  50. preMenuItem.setChecked(false);
  51. menuItem.setChecked(true);
  52. drawerLayout.closeDrawers();
  53. preMenuItem = menuItem;
  54.  
  55. // 不同item对应不同图片
  56. switch (menuItem.getItemId()) {
  57. case R.id.navigation_item1:
  58. imageView.setImageResource(R.mipmap.bg_one);
  59. break;
  60. case R.id.navigation_item2:
  61. imageView.setImageResource(R.mipmap.bg_two);
  62. break;
  63. case R.id.navigation_item3:
  64. imageView.setImageResource(R.mipmap.bg_three);
  65. break;
  66. case R.id.navigation_sub_item1:
  67. imageView.setImageResource(R.mipmap.bg_four);
  68. break;
  69. case R.id.navigation_sub_item2:
  70. imageView.setImageResource(R.mipmap.bg_five);
  71. break;
  72. case R.id.navigation_sub_item3:
  73. imageView.setImageResource(R.mipmap.bg_default);
  74. break;
  75. }
  76. return true;
  77. }
  78. });
  79. }
  80.  
  81. private void initView() {
  82. drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  83. imageView = (ImageView) findViewById(R.id.image);
  84. navigationView = (NavigationView) findViewById(R.id.navigation);
  85. }
  86.  
  87. }

activity_main.xml:

  1. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:id="@+id/drawer_layout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:fitsSystemWindows="true" >
  7.  
  8. <!-- 主界面 -->
  9.  
  10. <RelativeLayout
  11. android:id="@+id/parent_layout"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent" >
  14.  
  15. <ImageView
  16. android:id="@+id/image"
  17. android:layout_width="match_parent"
  18. android:layout_height="400dp"
  19. android:layout_centerInParent="true"
  20. android:padding="5dp"
  21. android:src="@mipmap/bg_default" />
  22.  
  23. <Button
  24. android:id="@+id/button"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_alignParentBottom="true"
  28. android:layout_centerHorizontal="true"
  29. android:paddingBottom="5dip"
  30. android:text="点击" />
  31.  
  32. <!-- app:itemIconTint 设置菜单图标颜色 app:itemTextColor设置菜单字体颜色 -->
  33. </RelativeLayout>
  34.  
  35. <android.support.design.widget.NavigationView
  36. android:id="@+id/navigation"
  37. android:layout_width="match_parent"
  38. android:layout_height="match_parent"
  39. android:layout_gravity="start"
  40. app:headerLayout="@layout/drawer_header"
  41. app:itemIconTint="#2196F3"
  42. app:itemTextColor="#3F51B5"
  43. app:menu="@menu/drawer_menu" >
  44. </android.support.design.widget.NavigationView>
  45.  
  46. </android.support.v4.widget.DrawerLayout>

drawer_header.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="180dp"
  5. android:background="@mipmap/bg_header"
  6. android:paddingBottom="15dp"
  7. android:paddingLeft="20dp" >
  8.  
  9. <ImageView
  10. android:id="@+id/photo"
  11. android:layout_width="80dp"
  12. android:layout_height="80dp"
  13. android:layout_above="@+id/desc"
  14. android:layout_marginBottom="20dp"
  15. android:src="@mipmap/photo" />
  16.  
  17. <TextView
  18. android:id="@+id/desc"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_alignParentBottom="true"
  22. android:layout_marginBottom="2dp"
  23. android:text="www.cnblogs.com/zzw1994"
  24. android:textColor="#ffffff"
  25. android:textSize="20sp"
  26. android:textStyle="bold" />
  27.  
  28. </RelativeLayout>

drawer_menu.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  3.  
  4. <group>
  5. <item
  6. android:id="@+id/navigation_item1"
  7. android:checkable="true"
  8. android:icon="@mipmap/icon_hot"
  9. android:title="热门图片"/>
  10. <item
  11. android:id="@+id/navigation_item2"
  12. android:checkable="true"
  13. android:icon="@mipmap/icon_people"
  14. android:title="联系人"/>
  15. <item
  16. android:id="@+id/navigation_item3"
  17. android:checkable="true"
  18. android:icon="@mipmap/icon_photos"
  19. android:title="图库"/>
  20. </group>
  21.  
  22. <item
  23. android:id="@+id/navigation_sub"
  24. android:title="新增功能">
  25. <menu>
  26. <item
  27. android:id="@+id/navigation_sub_item1"
  28. android:checkable="true"
  29. android:icon="@mipmap/icon_local"
  30. android:title="地图导航"/>
  31. <item
  32. android:id="@+id/navigation_sub_item2"
  33. android:checkable="true"
  34. android:icon="@mipmap/icon_event"
  35. android:title="最近热门"/>
  36. <item
  37. android:id="@+id/navigation_sub_item3"
  38. android:checkable="true"
  39. android:icon="@mipmap/icon_communities"
  40. android:title="社交圈子"/>
  41. </menu>
  42. </item>
  43.  
  44. </menu>

注意主题:

  1. <style name="DemoTheme" parent="Theme.AppCompat.Light.NoActionBar">
  2. </style>
  1. <application
  2.  
  3. android:theme="@style/DemoTheme" >
  4.  
  5. </application>

Android Material Design:NavigationView抽屉导航菜单的更多相关文章

  1. Android Material Design NavigationView 及 Palette 颜色提取器

    DrawerLayout + NavigationView DrawerLayout布局,通常在里面添加两个子控件,程序主界面添加到NavitagionView前面. <android.supp ...

  2. Android Material Design 兼容库的使用

    Android Material Design 兼容库的使用 mecury 前言:近来学习了Android Material Design 兼容库,为了把这个弄懂,才有了这篇博客,这里先推荐两篇博客: ...

  3. Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果

    前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...

  4. Android Material Design Ripple Effect在Android5.0(SDK=21)以下Android版本崩溃问题解决

    Android Material Design Ripple Effect在Android5.0(SDK=21)以下Android版本崩溃问题解决 附录1的Android Ripple Effect水 ...

  5. Android Material Design : Ripple Effect水波波纹荡漾的视觉交互设计

     Android Material Design : Ripple Effect水波波纹荡漾的视觉交互设计 Android Ripple Effect波纹荡漾效果,是Android Materia ...

  6. Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout

    如果是为了兼容低版本的Android系统,则需要引用Android Material Design的扩展支持库,我在之前的一篇文章张,较为详细的说明了如何导入Android Material Desi ...

  7. MaterialEditText——Android Material Design EditText控件

    MaterialEditText是Android Material Design EditText控件.可以定制浮动标签.主要颜色.默认的错误颜色等. 随着 Material Design 的到来, ...

  8. Android Material Design控件学习(二)——NavigationView的学习和使用

    前言 上次我们学习了TabLayout的用法,今天我们继续学习MaterialDesign(简称MD)控件--NavigationView. 正如其名,NavigationView,导航View.一般 ...

  9. Android Material Design之 NavigationView侧滑界面自定义 随笔

    一.侧滑界面Menu自定义: 在menu文件夹下新建activity_main_drawer.xml文件,自定义标题和icon: <?xml version="1.0" en ...

随机推荐

  1. Inno Setup设置NT服务

    原文地址:http://stackoverflow.com/questions/16922272/unknown-identifier-and-wpselectdir // Variables Glo ...

  2. poj 1789 Truck History 最小生成树

    点击打开链接 Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15235   Accepted:  ...

  3. 配置Tomcat数据源

    1.方式一:在server.xml中配置 1)tomcat安装路径下conf目录下的server.xml,在<GlobalNamingResources>和</GlobalNamin ...

  4. MongoDB基本操作命令

    由于工作需要,笔者这两天使用了一下MongoDB.真的很不习惯!但是确实好用,命令比mysql和sqlserver简单很多.在这里整理一些MongoDB的基本操作命令分享出来. 客户端的安装就不说了, ...

  5. OSChina中远程GIT仓库同步探索

    GIT平台在OSChina中的搭建帮了我们很大的忙,但如何将本地GIT仓库上传至OSChina的远程仓库,相信这是一个艰难的坎,今天我就在此总结我的成功经验,帮助大家,共同学习.由于条件有限,我全部的 ...

  6. OpenWRT TP_LINK703N 校园网 锐捷认证解决办法

    OpenWRT TP_LINK703N 校园网 锐捷认证解决办法 一.准备的工具 1)      SSH登录工具,推荐使用MobaXterm_Personal下载链接https://moba.en.s ...

  7. 学习练习 java面向对象存取款查询余额

    package com.hanqi; public class Account { String ZhangHao; double CunKuanYuE; Account(String ZhangHa ...

  8. typedef 与define 的区别

    typedef和#define的用法与区别   typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译 ...

  9. iPad用户使用Mac和Windows应用软件-记Parallels Access使用体验

    iPad用户使用Mac和Windows应用软件-记ParallelsAccess使用体验 用ipad远程连接win系统已不是新鲜事情,我们可以使用TeamViewer和OnLiveDesktopPlu ...

  10. Duilib学习笔记《07》— 资源加载

    Duilib的界面表现力能如此丰富,很大程度上得益于贴图描述的简单强大.通过之前的学习及参看相关例子,我们可以发现,在XML布局文件中,不管是窗体背景还是控件,都添加了对应的图片资源以此来美化界面.而 ...