Android Design Support Library——Navigation View
前沿
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的更多相关文章
- 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...
- Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...
- 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 ...
- Material Design 开发利器:Android Design Support Library 介绍
转自:https://blog.leancloud.cn/3306/ Android 5.0 Lollipop 是迄今为止最重大的一次发布,很大程度上是因为 material design —— 这是 ...
- Android Design Support Library 中控件的使用简单介绍(一)
Android Design Support Library 中控件的使用简单介绍(一) 介绍 在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android ...
- Android Design Support Library初探,NavigationView实践
前言 在前几天的IO大会上,Google带来了Android M,同时还有Android支持库的新一轮更新,其中更是增加一个全新的支持库Android Design Support Library,包 ...
- Material Design with the Android Design Support Library
Material Design with the Android Design Support Library 原文http://www.sitepoint.com/material-design-a ...
- Android Design Support Library使用详解
Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...
- Android开发学习之路-Android Design Support Library使用(CoordinatorLayout的使用)
效果图: 上面的这个图有两个效果是,一个是顶部的图片,在上滑之后会隐藏起来并且显示出一个ToolBar(ToolBar类似于ActionBar,但是只有ToolBar是兼容Material Desig ...
随机推荐
- Effective C++ 随笔(2)
条款5 了解c++默默编写并调用哪些函数 编译器自动生成的copy 构造函数,copy赋值操作符,析构函数,构造函数,这些都是public和inline的,此处inline的意思是他们的定义都是在头文 ...
- rhel5.4+oracle 10g rac
各种报错各种愁啊 ... 1> 不知道什么原因,在节点2执行root.sh 报错 .无解 . 还原虚拟机,重新安装 .唯一与以前不同的是,执行orainroot.sh后 接着在节点2执行.再去分 ...
- (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)
http://poj.org/problem?id=3186 Description FJ has purchased N (1 <= N <= 2000) yummy treats ...
- vc6中向vs2010迁移的几个问题
vc6版本支持的库编译:CJ60lib 1. 用vs2010打开CJ60库的源码的dsw,强制打开 (1)设置项目属性的语言 因为,如果代码字符的编码集不一样,则会出现函数冲定义,参数冲突等问题,这可 ...
- 13.1.DataGrid的增、删、改、查前台页面
公共js: 前台页面:
- Scala_模式匹配
模式匹配 简单匹配 Scala的模式匹配最常用于match语句中.下面是一个简单的整型值的匹配实例 object TestMatch { def main(args: Array[String]): ...
- Android-Java-死锁
死锁:程序不往下执行了,程序又没有结束,就一直卡在哪里: 在使用synchronized的时候要避免死锁,synchronized嵌套就可能会引发死锁,需要严格的检查代码,排除死锁发生的可能: 特意演 ...
- PSP个人项目耗时对比记录表:四则运算
Personal Software Process Stages Time(%) 计划 5 •估计这个任务需要多长时间 5 开发 60 •需求分析 5 •生成设计文档 5 ...
- ip网段变更
背景 公司网络跟集团靠拢,先走第一步:IP网段变更.从XX网段切换到OO网段 方法 1. 准备工作 a. 保证IPMI连接正常 b. 获得新IP并核对对应主机名.旧IP是否相符 2. 确认网卡名称 # ...
- 在Windows7系统上能正常使用的程序,Windows10运行后部分状态不能及时变更
这是最近在开发一个通信项目时遇到的问题,一开始以为是窗体样式的原因,把窗体换成系统窗体之后还是在Win10上不能正常使用,后面突然想到会不会是匹配原因,试了一下,结果真的就正常了. 问题:例如一个通信 ...