前沿

  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为例

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

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

<android.support.v4.widget.DrawerLayout xmlns: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:fitsSystemWindows="true"> <include layout="@layout/include_list_viewpager" /><!-- 展示内容区域的布局--> <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"
app:menu="@menu/drawer_view" /> </android.support.v4.widget.DrawerLayout>

  两个重要的属性

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

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

  nav_header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="?attr/colorPrimaryDark"
android:gravity="bottom"
android:orientation="vertical"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> </LinearLayout>

  drawer_view.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/nav_home"
android:icon="@drawable/ic_dashboard"
android:title="Home" />
<item
android:id="@+id/nav_messages"
android:icon="@drawable/ic_event"
android:title="Messages" />
<item
android:id="@+id/nav_friends"
android:icon="@drawable/ic_headset"
android:title="Friends" />
<item
android:id="@+id/nav_discussion"
android:icon="@drawable/ic_forum"
android:title="Discussion" />
</group> <item android:title="Sub items">
<menu>
<item
android:icon="@drawable/ic_dashboard"
android:title="Sub item 1" />
<item
android:icon="@drawable/ic_forum"
android:title="Sub item 2" />
</menu>
</item> </menu>

  (2)在代码中声明使用

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}

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

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

  ../valuse-v21/styles.xml

<resources>

    <style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style> </resources>

  同时在DrawerLayout中加入如下属性

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. 如何批量下载网站中的超链接(一次性下载网页中所有可能的PDF文件)

    最近公司在做工程项目,实现文件批量下载. 网上找了很久,发现网上的代码都有相似的问题,不过最终还是让我找到了一个符合的项目. 工程: 进行项目文件下载功能分析,弄清楚文件批量下载的原理,提供的数据支持 ...

  2. Windows API编程(一)完整的示范程序

    ## #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//回调函数; int APIEN ...

  3. (转)Eclipse开发Web项目

    1. 建立最简单的JSP和servlet http://wenku.baidu.com/link?url=bcf8iwB3E5_gjl46WfZAekQUWsps0-G3MAbbKz5totQcvmS ...

  4. MV人物抹去

    填充为背景 Music Video is one type of Video Processing.视频剪辑反向复原:视频图像分类:TensorFlow图像分类教程   改起个什么名字 视频分类数据集 ...

  5. web-day5

    第5章WEB05- BootStrap篇 今日任务 使用JQuery完成表单校验 使用BootStrap制作一个响应式页面 使用BootStrap制作网站首页 教学导航 教学目标 掌握什么是响应式及响 ...

  6. 最长上升子序列 and 最长公共子序列 问题模板

    两种求最长上升子序列问题 第一种:定义dp[i]=以a[i]为末尾的最长上升子序列问题的长度 第二种:定义dp[i]=长度为i+1的上升 子序列 中末尾元素的最小值 #include <cstd ...

  7. 2018/6/29 关于hashmap的总结

    hashMap和ConcurrentHashMap的区别 hashMap内部具体如何实现的 如果hashMap的key是一个自定义的类,怎么办 为什么重写equals还要重写hashcode 一.什么 ...

  8. 一不小心发现了个Asp.Net Bug

    1. Ver是页面定义的变量 2. asp.net 页面定义为  <link href="/company/them/page.css?v=<%=Ver%>" r ...

  9. System.Data.SQLite安装的相关问题

    在使用System.Data.SQLite的过程中,遇到各种问题,特此记录下.(都被搞的折寿了,不仔细看文档的下场!) 1.选对.net Framework的版本. 2.X64和X86的问题,如果项目 ...

  10. 【js】关于正则表达式

    正则表达式描述了字符的模式对象 语法: var patt=new RegExp(pattern,modifiers); 或更简单的方法 var patt=/pattern/modifiers; 模式描 ...