【Android - 控件】之MD - NavigationView的使用
NavigationView是Android 5.0新特性——Material Design中的一个布局控件,可以结合DrawerLayout使用,让侧滑菜单变得更加美观(可以添加头部布局)。
NavigationView需要嵌套在DrawerLayout内部,其相对于单独使用DrawerLayout的优点在于可以额外添加一个HeaderView头部布局。另外,NavigationView中的其他选项都是用menu的形式来编写的,menu中的分支也可以在NavigationView中形成分栏效果。
和其他MD控件一样,使用NavigationView需要在gradle文件中注册依赖:
compile 'com.android.support:design:24.1.1'
1、NavigationView的属性:
app:headerLayout:NavigationView的头部布局,其中可以存放ImageView、TextView等控件
app:menu:NavigationView中的Item项,存在menu中
2、布局示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:orientation="vertical"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize" /> <android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!-- 主界面布局 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DDDDDD" /> <android.support.design.widget.NavigationView
android:id="@+id/navigationview"
android:layout_width="256.0dip"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
app:headerLayout="@layout/sideworks_navhead"
app:menu="@menu/navigate" /> </android.support.v4.widget.DrawerLayout>
</LinearLayout>
menu文件中的代码如下:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group>
<item
android:id="@+id/one_one"
android:icon="@mipmap/ic_launcher"
android:title="Group one Item one" />
<item
android:id="@+id/one_two"
android:icon="@mipmap/ic_launcher"
android:title="Group one Item two" />
<item
android:id="@+id/one_three"
android:icon="@mipmap/ic_launcher"
android:title="Group one Item three" />
</group>
<item android:title="Group Two">
<menu>
<item
android:id="@+id/two_one"
android:icon="@mipmap/ic_launcher"
android:title="Group one Item one" />
<item
android:id="@+id/two_two"
android:icon="@mipmap/ic_launcher"
android:title="Group one Item two" />
</menu>
</item>
</menu>
头部布局中的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="center_horizontal"
android:orientation="vertical"> <ImageView
android:id="@+id/header_image"
android:layout_width="150.0dip"
android:layout_height="150.0dip"
android:layout_marginTop="20.0dip"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_launcher" /> <TextView
android:id="@+id/header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20.0dip"
android:layout_marginTop="10.0dip"
android:text="Navigation View"
android:textColor="#FFFFFFFF"
android:textSize="18.0sp"
android:textStyle="bold" /> </LinearLayout>
3、弹出抽屉:
在JAVA代码中通过点击Toolbar中的ActionBarDrawerToggle来弹出抽屉。代码如下:
// 绑定Toolbar到Activity中
setSupportActionBar(toolbar);
// 在Toolbar上设置一个按钮,点击这个按钮可以拉出抽屉
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(MainActivity.this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
toggle.syncState();
drawer.addDrawerListener(toggle);
最终的演示效果如下图:
以上就是对NavigationView的简单用法的介绍,下面贴出码云上的源码,供大家参考。
【Android - 控件】之MD - NavigationView的使用的更多相关文章
- RxJava RxBinding RxView 控件事件 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- [Android Pro] android控件ListView顶部或者底部也显示分割线
reference to : http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...
- Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像
此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...
- Android 控件架构及View、ViewGroup的测量
附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...
- Android - 控件android:ems属性
Android - 控件android:ems属性http://blog.csdn.net/caroline_wendy/article/details/41684255?utm_source=tui ...
- Android 控件知识点,
一.Android控件具有visibility属性,可以取三个值:visible(默认值)可见,invisible(不可见,但仍然占据原有的位置和大小,可以看做是变得透明了),gone(空间不仅不可见 ...
- UIAutomator定位Android控件的方法
UIAutomator各种控件定位的方法. 1. 背景 使用SDK自带的NotePad应用,尝试去获得在NotesList那个Activity里的Menu Options上面的那个Add note菜单 ...
- 从Android系统出发,分析Android控件构架
从Android系统出发,分析Android控件构架 Android中所有的控件追溯到根源,就是View 和ViewGroup,相信这个大家都知道,但是大家也许会不太清楚它们之间的具体关系是什么,在A ...
- Android控件系列之RadioButton&RadioGroup(转)
学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...
- 第三个 android控件
android控件以及控件对应的属性:
随机推荐
- MySQL、Oracle、SqlServer的区别
鉴于和数据库打交道日益频繁,遂决定写一篇关于Oracle.SqlServer.MySQL区别的个人观点. MySQL是大学时的主要学习对象,但刚参加工作时转到了SqlServer,现在主要接触的是Or ...
- 第二十六章 system v消息队列(二)
msgsnd int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); 作用: 把一条消息添加到消息队列中 参数: msqi ...
- 关于css里大于号(>)的用法
之前用的css没涉及到这个问题,今天看到.知道大概用法,但不知道和普通的后代选择器有什么区别.到网上找了,其实w3c的css文档里有很详细明确的介绍(http://www.w3school.com.c ...
- C函数库stdio.h概况
库变量 size_t 这是无符号整数类型,它是 sizeof 关键字的结果. FILE 这是一个适合存储文件流信息的对象类型. fpos_t 这是一个适合存储文件中任何位置的对象类 ...
- 深度学习tensorflow实战笔记(2)图像转换成tfrecords和读取
1.准备数据 首选将自己的图像数据分类分别放在不同的文件夹下,比如新建data文件夹,data文件夹下分别存放up和low文件夹,up和low文件夹下存放对应的图像数据.也可以把up和low文件夹换成 ...
- Nginx篇--最初级用法web
最近很久都没有写博客了,一来主要是时间不够每天回到家都接近晚上11点了,但是以后每天还是保证一篇随笔.好用来整理总结自己的知识. web服务器很有多例如:Apache nginx tengine li ...
- CSPS模拟 54
T1 $2^{联通块个数}-2$ T2 $bitset$随便水 T3 分类讨论,讨论,讨论
- STL.h
最近老是被系统的一些STL卡到飞起,然后就手打了一个STL.h 库函数还没有打完,以后打新的还会再更,大家也可以使用,顺便帮我找一下bug,然后我再改进! template< typename ...
- egret开发方法(最笨的方法)
egret开发方法(最笨的方法)1 1个精灵1个对象名字 获取精灵设置属性也是直接获取对象设置属性 (不用想的少些代码 因为没有jquery好用) ps:如果要设置很多个精灵属性 那可以添加到数组 然 ...
- 解决mybatis中 数据库column 和 类的属性名property 不一致的两种方式
解决方式way1:resultMap (1)studentMapper.xml <!-- 当数据库的字段名 和 类的属性名 不一致的时候的解决方式:2种 way1--> <selec ...