Android开发之NavigationView的使用
NavigationView主要是和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">
<!-- Your contents -->
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/my_navigation_items" />
</android.support.v4.widget.DrawerLayout>
在使用NavigationView之前需要将相应的design库添加到项目的依赖中,
然后在xml中外层用DrawerLayout包裹,内层一部分是正文内容区(content),另一部分则是侧边栏NavigationView,这里面包含两个布局,一个是headerLayout,一个是menu,
头布局没什么好说的,跟普通的布局定义方式类似,menu布局的示例代码如下
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<group android:checkableBehavior="single">
<item android:id="@+id/navigation_item_first" android:icon="@drawable/first" android:title="第一项" />
<item android:id="@+id/navigation_item_second" android:icon="@drawable/second" android:title="第二项" />
<item android:id="@+id/navigation_item_third" android:icon="@drawable/third" android:title="第三项" />
</group>
</menu>
其中各个条目还有checked属性,设置为true的条目将会高亮显示
group中的checkableBehavior属性表示这组这组菜单是否checkable,有三个值可选,分别为none,all(单选/单选按钮radio button),single(非单选/复选类型checkboxes)
最后还可以在代码中通过setNavigationItemSelectedListener方法来设置菜单项被点击的回调,
里面重写的onNavigationItemSelectedListener方法提供了被选中的MenuItem,用法和Activity的onOptionsItemSelected类似
这样就可以实现一个简单的抽屉导航侧滑菜单栏,需要更多的属性与用法可以自行查看Android官方文档并深入钻研
Android开发之NavigationView的使用的更多相关文章
- Android开发之Java集合类性能分析
对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和 Map这三大类的集合,今天Android吧(ard8. ...
- Android开发之InstanceState详解
Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...
- Android开发之Git配置
Android开发之Git配置 1.首先git配置: 输入命令: git config --global user.name "xxx.xx" git config --globa ...
- 【Android UI】Android开发之View的几种布局方式及实践
引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...
- Android开发之旅: Intents和Intent Filters(理论部分)
引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...
- Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab
今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...
- Android开发之Java必备基础
Android开发之Java必备基础 Java类型系统 Java语言基础数据类型有两种:对象和基本类型(Primitives).Java通过强制使用静态类型来确保类型安全,要求每个变量在使用之前必须先 ...
- Android开发之PopupWindow
/* * Android开发之PopupWindow * * Created on: 2011-8-8 * Author: blueeagle * Email: liujiaxiang@g ...
- [置顶] Android开发之MediaPlayerService服务详解(一)
前面一节我们分析了Binder通信相关的两个重要类:ProcessState 和 IPCThreadState.ProcessState负责打开Binder 驱动,每个进程只有一个.而 IPCThre ...
随机推荐
- 第七届蓝桥杯javaB组真题解析-煤球数目(第一题)
题目 /* 煤球数目 有一堆煤球,堆成三角棱锥形.具体: 第一层放1个, 第二层3个(排列成三角形), 第三层6个(排列成三角形), 第四层10个(排列成三角形), .... 如果一共有100层,共有 ...
- We Chall-Training: Encodings I -Writeup
MarkdownPad Document html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,ab ...
- 使用T4模板生成POCO类
为什么叫T4?因为简写为4个T. T4(Text Template Transformation Toolkit)是微软官方在VisualStudio 2008中开始使用的代码生成引擎.在 Visua ...
- JavaScript中的this关键字的用法和注意点
JavaScript中的this关键字的用法和注意点 一.this关键字的用法 this一般用于指向对象(绑定对象); 01.在普通函数调用中,其内部的this指向全局对象(window); func ...
- [JAVA] - Java OutOfMemoryError分类
Java OutOfMemoryError一般常遇到的分为两类,分别提示: "Java heap space" 和 "PermGen space",前面的是指j ...
- [2017.02.21] 《Haskell趣学指南 —— Learning You a Haskell for Great Good!》
{- 2017.02.21 <Haskell趣学指南 -- Learning You a Haskell for Great Good!> [官网](http://learnyouahas ...
- 学生管理系统(C语言)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 3 #define LEN ...
- 安装MongoDB x86_64
1.下载MongoDB 下载mongodb的windows版本,有32位和64位版本,根据系统情况下载,下载地址:http://www.mongodb.org/downloads 2.安装MongoD ...
- UIView和layer的关系
UIView和layer的关系 UIView是layer的代理 View的根layer ---也就是说view本身自带一个layer, 这个layer 我们叫它根layer 所有视图显示是因为继承UI ...
- Servlet支持上传多张图片
首先前端的表单是这个形式: <form target="_self" method="post" action="fileUploadMult ...