今天来看一下 NavigationView 的使用,NavigationView 是一个标准的导航菜单,其菜单内容由菜单资源文件来填充,NavigationView 一般和 DrawerLayout 一起搭配使用构成抽屉菜单,分别由内容页和菜单页组成。

基本布局

可以直接使用 DrawerLayout 作为根布局,里面依次是内容布局和菜单布局,切记内容布局一定是在菜单布局的前面,可以这样理解菜单划出的时候肯定应该在内容布局之上,如果两者顺序相反,则影响菜单 Item 的点击事件以及菜单的滑动隐藏,当然如果有 ToolBar 等,可以按需添加到内容布局中,也可以 DrawerLayout 外,唯一区别是侧换菜单是否遮挡 ToolBar,基本使用参考如下:

 <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"> <!--内容 --> <!--菜单-->
<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 的常用属性,具体如下:

<!--菜单弹出方向-->
android:layout_gravity="start"
<!--菜单图标渲染的颜色-->
app:itemIconTint="@color/colorPrimary"
<!--菜单文字的颜色-->
app:itemTextColor="@color/colorNormal"
<!--菜单项背景颜色(组之间有间隔)-->
app:itemBackground="@color/colorBackground"
<!--菜单项-->
app:menu="@menu/menu_navigation_view"
<!--NavigationView的头部布局-->
app:headerLayout="@layout/head_navigation_layout"

文字选中效果

如果美工比较用心会告诉点击时是那种颜色、按下是那种颜色或者是某种效果,此时就需要设置菜单项文字选中效果了,这里选择创建在 color 资源文件的形式来实现文字选中效果了,定义 color 资源文件如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--按下-->
<item android:color="@color/colorPress" android:state_pressed="true"/>
<!--选中-->
<item android:color="@color/colorCheck" android:state_checked="true"/>
<!--默认-->
<item android:color="@color/colorNormal"/>
</selector>

然后,设置 NavigationView 的 itemTextColor 属性即可,具体如下:

<!--设置菜单项颜色-->
app:itemTextColor="@color/select_color_navigation"

当然,也可以在代码中设置,具体如下:

//设置菜单项颜色
ColorStateList colorStateList = getResources().getColorStateList(R.color.select_color_navigation);
navigationView.setItemTextColor(colorStateList);

然后,设置对 NavigationView 菜单项选中的事件监听,具体如下:

navigationView.setNavigationItemSelectedListener(this);

最后,在点击完成要设置该菜单项已被选中,具体如下:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()){
case R.id.menu1:
Toast.makeText(this,"menu1",Toast.LENGTH_SHORT).show();
break;
//...
}
//设置菜单项选中
item.setCheckable(true);
//关闭Drawer
// navDrawLayout.closeDrawers();
return true;
}

图标与文字间距

使用 NavigationView 之后发现,菜单图标与菜单文字之间有一定的间距,看着有点宽,有一点强迫症必须稍微修改小一点,根据之前修改 FloatingActionButton 大小的经验,翻一下源码找设置这个间距的位置,查看 NavigationView 源码,最终在 NavigationMenuPresenter 中找到相关的 dimen 值,然后顺藤摸瓜找到与 Navigation 相关的几个 dimen 值,具体如下:

public static final int design_navigation_elevation = 0x7f060064;
public static final int design_navigation_icon_padding = 0x7f060065;
public static final int design_navigation_icon_size = 0x7f060066;
public static final int design_navigation_max_width = 0x7f060067;
public static final int design_navigation_padding_bottom = 0x7f060068;
public static final int design_navigation_separator_vertical_padding = 0x7f060069;

此时,在项目的 dimens 文件夹中创建名称相同的值覆盖即可,这里是修改 Menu 图标与文字之间的间距,所以我们只要设置:

<!--修改NavigationView菜单图标与文字之间的间距-->
<dimen name="design_navigation_icon_padding" tools:override="true">10dp</dimen>

至于其他相关的 dimen 值就不一一说明了,这样就修改了 Menu 图标与文字之间的间距。

案例

下面是一个 NavigationView 结合 DrawerLayout 的使用案例,布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--ToolBar-->
<android.support.v7.widget.Toolbar
android:id="@+id/navToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary" /> <android.support.v4.widget.DrawerLayout
android:id="@+id/navDrawLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--内容-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/app_name"
android:textSize="18sp" />
</LinearLayout>
<!--菜单-->
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/head_navigation_layout"
app:itemIconTint="@color/colorPrimary"
app:itemTextColor="@color/select_color_navigation"
app:menu="@menu/menu_navigation_view" />
</android.support.v4.widget.DrawerLayout> </LinearLayout>

显示效果

下面是显示效果,如下图所示:

最近总结了一下 Material Design 组件的使用,可以选择关注微信公众号:jzman-blog 获取最新更新,可以在公众号回复关键字 MD 获取相关代码链接。

Material Design 组件之NavigationView的更多相关文章

  1. Material Design 组件之 CollapsingToolbarLayout

    CollapsingToolbarLayout 主要用于实现一个可折叠的标题栏,一般作为 AppBarLayout 的子 View 来使用,下面总结一下 CollapsingToolbarLayout ...

  2. Material Design 组件之 FloatingActionButton

    Material Design 设计规范在 Google I/O 2014 推出,这种设计理念一经推出就受到广大开发者的喜爱,主要侧重于纸墨化创作和突出设计的实体感,使得设计更接近于真实世界,力求平滑 ...

  3. Material Design 组件之 AppBarLayout

    AppBarLayout 是一个垂直方向的 LinearLayout,它实现了许多符合 Material Design 设计规范的状态栏应该具有的功能,比如滚动手势. AppBarLayout 一般直 ...

  4. Material Design with the Android Design Support Library

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

  5. Jquery之家5个顶级Material Design框架

    谷歌Material Design在如今的前端页面设计中非常流行.Material Design的设计风格向我们展示了一个简单而有内涵的现代UI设计方案. Material Design是如此的简洁美 ...

  6. Top 15 - Material Design框架和类库(译)

    _Material design_是Google开发的,目的是为了统一公司的web端和手机端的产品风格.它是基于很多的原则,比如像合适的动画,响应式,以及颜色和阴影的使用.完整的指南详情请看这里(ht ...

  7. Material Design Lite,简洁惊艳的前端工具箱 之 交互组件。

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接, 博客地址为http://www.cnblogs.com/jasonnode/ . 网站上有对应 ...

  8. 基于React Native的Material Design风格的组件库 MRN

    基于React Native的Material Design风格的组件库.(为了平台统一体验,目前只打算支持安卓) 官方网站 http://mrn.js.org/ Github https://git ...

  9. Material Design: NavigationView FlaotingActionBar SnackBar采用

    转载 请明确说明 MingsangAndroid 本文介绍了Design Support Library的引入 拥抱Android Design Support Library新变化(导航视图.悬浮A ...

随机推荐

  1. 前端每日实战:60# 视频演示如何用纯 CSS 创作一块乐高积木

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/qKKqrv 可交互视频 此视频是可 ...

  2. Python-Pyquery库的安装和调用

    解析库pyquery:# pip安装pyquery库pip3 install pyquery from pyquery import PyQuery as pq # 定义doc,输入html源代码 d ...

  3. 汇编语言-[bx]和loop指令和多个段

    5.1 [BX]和内存单元的描述 要完成描述一个内存单元,需要两种信息: 内存单元的地址: 可以用 [0] 表示一个内存单元, 0 表示单元的偏移地址,段地址默认在 ds 中: 同样也可以用 [bx] ...

  4. 进程,线程,Event Loop(事件循环),Web Worker

    线程,是程序执行流的最小单位.线程可与同属一个进程的其他线程共享所拥有的全部资源,同一进程中的多个线程之间可以并发执行.线程有就绪,阻塞,运行三种基本状态. 阮一峰大神针对进程和线程的类比,很是形象: ...

  5. python使用for循环打印9*9乘法表。

    代码如下: for a in range(1, 10): for b in range(1, 10): if b <= a: print("%d*%d=%d\t" % (b, ...

  6. 看逐浪CMS技术小哥做SVG动画(附使用Bodymovin和Lottie将Adobe After Effects(AE)程式转为 HTML5/Android/iOS原生的动画全过程-即AE转svg\canvas\html5动画)

      名词解解释 adobe After Effects AE:adobe After Effects,adobe公司的专业视频制作软件. Bodymovin插件预览 Bodymovin:是一个AE的插 ...

  7. openwrt 外挂usb 网卡 RTL8188CU 及添加 RT5572 kernel支持

    RT5572 原来叫 Ralink雷凌 现在被 MTK 收购了,淘宝上买的很便宜50块邮,2.4 5G 双频.在 win10 上插了试试,果然是支持 5G.这上面写着 飞荣 是什么牌子,有知道的和我说 ...

  8. openwrt sdk 添加软件包 Makefile 写法

    参考 https://openwrt.org/start?id=docs/guide-developer/packages ,英文稍好点的自己看吧,我写出来也就是方便,英文不好的人看. 软件包的来源, ...

  9. 9-4 Vue 缓存和子传副(组件)方法绑定

    学习了Vue框架近三个月,现在对数据绑定有了点认识,但是发现自己反而对js不是特别的熟. 下面是今天写代码刚好碰到的问题: 缓存的话:3句代码 sessionStorage.setItem(" ...

  10. c# 导出excel的两种常见方法

    1,不是用第三方插件(html直接输出) StringBuilder ssb = new StringBuilder(); StringBuilder sb = new StringBuilder() ...