前沿

  Android 从5.0开始引入了Material design元素的设计,这种新的设计语言让整个安卓的用户体验焕然一新,google在Android Design Support Library中封装了一些重要的material design控件,在这之前其实github上也已经出现了许多各种各样的material design 控件,只不过现在google把有些控件标准化了,注意这个Android Design Support Library和Android Support Library是有区别的,Android Support Library 只是支持了一些基本控件的封装,而Android Design Support Library主要是一些Material design特效的实现,Android Design Support Library包括以下八种控件:

  Navigation View——抽屉导航

  TextInputLayout——EditText悬浮标签

  Floating Action Button——悬浮操作按钮

  Snackbar——提示(类似Toast)

  TabLayout——选项卡

  CoordinatorLayout——滚动控制

  CollapsingToolbarLayout——可折叠的Toolbar(Google+、photos中的效果)

  AppBarLayout——容器AppBar

  本文分章对以上控件主要做使用方式介绍,暂不分析源码。首先看Navigation View

  Navigation View——抽屉导航

  非常实用的一种抽屉导航效果,支持直接通过菜单资源文件直接生成导航标签,实现起来也非常简单,效果如下图所示:

  

  使用步骤:

  (1)app的build.gradle中引入design包本文design包版本以23.1.1为例

  1. dependencies {
  2. compile 'com.android.support:design:23.1.1'
  3. }

  (2)xml布局文件(activity_main.xml)和SlideMenu一样需要使用到DrawerLayout。

  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. <include layout="@layout/include_list_viewpager" /><!-- 展示内容区域的布局-->
  9.  
  10. <android.support.design.widget.NavigationView
  11. android:id="@+id/nav_view"
  12. android:layout_width="wrap_content"
  13. android:layout_height="match_parent"
  14. android:layout_gravity="start"
  15. android:fitsSystemWindows="true"
  16. app:headerLayout="@layout/nav_header"
  17. app:menu="@menu/drawer_view" />
  18.  
  19. </android.support.v4.widget.DrawerLayout>

  两个重要的属性

  app:headerLayout ——导航头布局
  app:menu——导航菜单布局

  layout_gravity——用来控制左滑和右滑的,start左滑,end右滑,其实这个是跟DrawerLayout有关的,具体可参考DrawerLayout的使用。

  nav_header.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="192dp"
  4. android:background="?attr/colorPrimaryDark"
  5. android:gravity="bottom"
  6. android:orientation="vertical"
  7. android:padding="16dp"
  8. android:theme="@style/ThemeOverlay.AppCompat.Dark">
  9.  
  10. <TextView
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:text="Username"
  14. android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
  15.  
  16. </LinearLayout>

  drawer_view.xml(重点是看如何实现分组的)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. <group android:checkableBehavior="single"><!-- 实现单选-->
  4. <item
  5. android:id="@+id/nav_home"
  6. android:icon="@drawable/ic_dashboard"
  7. android:title="Home" />
  8. <item
  9. android:id="@+id/nav_messages"
  10. android:icon="@drawable/ic_event"
  11. android:title="Messages" />
  12. <item
  13. android:id="@+id/nav_friends"
  14. android:icon="@drawable/ic_headset"
  15. android:title="Friends" />
  16. <item
  17. android:id="@+id/nav_discussion"
  18. android:icon="@drawable/ic_forum"
  19. android:title="Discussion" />
  20. </group>
  21.  
  22. <item android:title="Sub items">
  23. <menu>
  24. <item
  25. android:icon="@drawable/ic_dashboard"
  26. android:title="Sub item 1" />
  27. <item
  28. android:icon="@drawable/ic_forum"
  29. android:title="Sub item 2" />
  30. </menu>
  31. </item>
  32.  
  33. </menu>

  (2)在代码中声明使用

  1. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  2. if (navigationView != null) {
  3. navigationView.setNavigationItemSelectedListener(
  4. new NavigationView.OnNavigationItemSelectedListener() {
  5. @Override
  6. public boolean onNavigationItemSelected(MenuItem menuItem) {
  7. menuItem.setChecked(true);
  8. mDrawerLayout.closeDrawers();
  9. return true;
  10. }
  11. });
  12. }

  在onNavigationItemSelected()方法中就可以获取导航菜单中的每个Item进而实现相应的功能了。

  扩展:如果你想让你的导航菜单在status bar 上也显示需要进行如下设置主要针对5.0及以上版本

  ../valuse-v21/styles.xml

  1. <resources>
  2.  
  3. <style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo">
  4. <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  5. <item name="android:statusBarColor">@android:color/transparent</item>
  6. </style>
  7.  
  8. </resources>

  同时在DrawerLayout中加入如下属性

  1. android:fitsSystemWindows="true"

  如果你感觉Navigation View还不够强大,可以看看MaterialDrawer,连接如下:https://github.com/mikepenz/MaterialDrawer

Android Design Support Library——Navigation View的更多相关文章

  1. 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...

  2. Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...

  3. Codelab for Android Design Support Library used in I/O Rewind Bangkok session

    At the moment I believe that there is no any Android Developer who doesn't know about Material Desig ...

  4. Material Design 开发利器:Android Design Support Library 介绍

    转自:https://blog.leancloud.cn/3306/ Android 5.0 Lollipop 是迄今为止最重大的一次发布,很大程度上是因为 material design —— 这是 ...

  5. Android Design Support Library 中控件的使用简单介绍(一)

    Android Design Support Library 中控件的使用简单介绍(一) 介绍 在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android ...

  6. Android Design Support Library初探,NavigationView实践

    前言 在前几天的IO大会上,Google带来了Android M,同时还有Android支持库的新一轮更新,其中更是增加一个全新的支持库Android Design Support Library,包 ...

  7. Material Design with the Android Design Support Library

    Material Design with the Android Design Support Library 原文http://www.sitepoint.com/material-design-a ...

  8. Android Design Support Library使用详解

    Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...

  9. Android开发学习之路-Android Design Support Library使用(CoordinatorLayout的使用)

    效果图: 上面的这个图有两个效果是,一个是顶部的图片,在上滑之后会隐藏起来并且显示出一个ToolBar(ToolBar类似于ActionBar,但是只有ToolBar是兼容Material Desig ...

随机推荐

  1. Effective C++ 随笔(2)

    条款5 了解c++默默编写并调用哪些函数 编译器自动生成的copy 构造函数,copy赋值操作符,析构函数,构造函数,这些都是public和inline的,此处inline的意思是他们的定义都是在头文 ...

  2. rhel5.4+oracle 10g rac

    各种报错各种愁啊 ... 1> 不知道什么原因,在节点2执行root.sh 报错 .无解 . 还原虚拟机,重新安装 .唯一与以前不同的是,执行orainroot.sh后 接着在节点2执行.再去分 ...

  3. (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)

    http://poj.org/problem?id=3186   Description FJ has purchased N (1 <= N <= 2000) yummy treats ...

  4. vc6中向vs2010迁移的几个问题

    vc6版本支持的库编译:CJ60lib 1. 用vs2010打开CJ60库的源码的dsw,强制打开 (1)设置项目属性的语言 因为,如果代码字符的编码集不一样,则会出现函数冲定义,参数冲突等问题,这可 ...

  5. 13.1.DataGrid的增、删、改、查前台页面

    公共js: 前台页面:

  6. Scala_模式匹配

    模式匹配 简单匹配 Scala的模式匹配最常用于match语句中.下面是一个简单的整型值的匹配实例 object TestMatch {  def main(args: Array[String]): ...

  7. Android-Java-死锁

    死锁:程序不往下执行了,程序又没有结束,就一直卡在哪里: 在使用synchronized的时候要避免死锁,synchronized嵌套就可能会引发死锁,需要严格的检查代码,排除死锁发生的可能: 特意演 ...

  8. PSP个人项目耗时对比记录表:四则运算

    Personal Software Process Stages Time(%) 计划 5    •估计这个任务需要多长时间 5 开发 60    •需求分析  5    •生成设计文档  5     ...

  9. ip网段变更

    背景 公司网络跟集团靠拢,先走第一步:IP网段变更.从XX网段切换到OO网段 方法 1. 准备工作 a. 保证IPMI连接正常 b. 获得新IP并核对对应主机名.旧IP是否相符 2. 确认网卡名称 # ...

  10. 在Windows7系统上能正常使用的程序,Windows10运行后部分状态不能及时变更

    这是最近在开发一个通信项目时遇到的问题,一开始以为是窗体样式的原因,把窗体换成系统窗体之后还是在Win10上不能正常使用,后面突然想到会不会是匹配原因,试了一下,结果真的就正常了. 问题:例如一个通信 ...