1. NavigationView的使用

这里我们来讲讲在Android5.0之后推出的NavigationView的具体使用方式。和普通的侧拉菜单制作方式一样,首先所有的东西还是都放在一个DrawerLayout中。如果对DrawerLayout的用法还不是很了解的。

可以参考我上一篇文章:Android——MaterialDesign之二DrawerLayout

    CircleImageView实现图片圆形化的功能,我是感觉非常的nice,很好用。具体看下面例子操作。

首先添加库

Nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_call"
        android:icon="@mipmap/call_1"
        android:title="Call">
    </item>

<item
        android:id="@+id/nav_friend"
        android:icon="@mipmap/friend_1"
        android:title="Friend">
    </item>
    <item
        android:id="@+id/nav_location"
        android:icon="@mipmap/location_1"
        android:title="Location">
    </item>
    <item
        android:id="@+id/nav_mail"
        android:icon="@mipmap/mail_1"
        android:title="Mail">
    </item>
    <item
        android:id="@+id/nav_task"
        android:icon="@mipmap/task_1"
        android:title="Task">
    </item>
</menu>

这里是侧滑的menu包括电话、朋友、位置、邮件、日程。

Nav_header.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="180dp"
    android:padding="10dp"
    android:background="?attr/colorPrimary">

<de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@mipmap/p4"
        android:layout_centerInParent="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mail"
        android:layout_alignParentBottom="true"
        android:textSize="14sp"
        android:textColor="#fff"
        android:text="111222@qq.com"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/username"
        android:text="Tom Matin"
        android:textSize="14sp"
        android:textColor="#fff"
        android:layout_above="@+id/mail"/>
</RelativeLayout>

详解:这里用到了CircleImageViewsrc加入图片,自动的会处理图片圆形化

 

Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    tools:context="com.example.materialtest_demo2.MainActivity">

<android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_menu"
        app:headerLayout="@layout/nav_header"
        android:id="@+id/nav_view">
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

详解:这里侧滑还是用到了上次讲解的DrawerLayout 进行侧滑,而NavigationView代替了上次的TextView,通过 app:menuapp:headerLayout设置属性。

mainActivity.class:

...
//侧滑菜单
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

navigationView.setCheckedItem(R.id.nav_call);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        return true;
    }
});

...

详解:这里获取到navigationView的实例后调用setCheckedItem()方法设置默认Call选项为选中,接着调用设置监听器,进行我们想要的操作,这里就简单的关闭菜单。

实验效果如下:

Android——MaterialDesign之三NavigationView的更多相关文章

  1. Android单元测试之三:使用模拟框架模拟依赖

    Android单元测试之三:使用模拟框架模拟依赖 基本描述 如果是一些工具类方法的测试,如计算两数之和的方法,本地 JVM 虚拟机就能提供足够的运行环境,但如果要测试的单元依赖了 Android 框架 ...

  2. Android——MaterialDesign之二DrawerLayout

    滑动菜单--DrawerLayout 滑动菜单就是把一些菜单选项隐藏起来,而不是放置主屏幕中,然后可以通过滑动的方式将菜单显示出来,具有非常的画面效果,就是类似侧边滑动. 例子:需要上一次的Toolb ...

  3. Android Material Design:NavigationView抽屉导航菜单

    需要添加的包: 测试代码: package com.zzw.navigationview; import android.app.Activity; import android.os.Bundle; ...

  4. Android Material Design NavigationView 及 Palette 颜色提取器

    DrawerLayout + NavigationView DrawerLayout布局,通常在里面添加两个子控件,程序主界面添加到NavitagionView前面. <android.supp ...

  5. Android 自己实现 NavigationView [Design Support Library(1)]

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46405409: 本文出自:[张鸿洋的博客] 一.概述 Google I/O 2 ...

  6. Android开发之NavigationView的使用

    NavigationView主要是和DrawerLayout框架结合使用,来完成抽屉导航实现侧边栏 引用一段官方文档的示例代码 <android.support.v4.widget.Drawer ...

  7. Android——MaterialDesign之一Toolbar

    Toolbar 由于ActionBar设计原因只能存在活动的顶部,从而不能实现MaterialDesign的效果,现在推荐使用Toolbar,继承Actionbar,但是比起它更加的灵活. 设置主题: ...

  8. .Net程序员玩转Android系列之三~快速上手(转)

    转自http://www.cnblogs.com/HouZhiHouJueBlogs/p/3962122.html 快速环境搭建和Hello World 第一步:JAVA SDK(JDK)的安装: 官 ...

  9. Android开发之三种动画

    转载:http://www.cnblogs.com/angeldevil/archive/2011/12/02/2271096.html http://www.lightskystreet.com/2 ...

随机推荐

  1. pycharm企业版注册码

    pycharm下载最新版 链接:https://pan.baidu.com/s/1gKOCf3PQFc1_2amkMUU1-A 提取码:9pt0 下载企业版: http://www.jetbrains ...

  2. Python:Day45 Javascript的String字符串

    typeof只能判断普通数据类型, 对于复杂的只是判断出来是一个Object: instanceof 可以判断数据是否是某一类型: alert(s instanceof String); String ...

  3. 2017-2018-2 20155314《网络对抗技术》Exp7 网络欺诈防范

    2017-2018-2 20155314<网络对抗技术>Exp7 网络欺诈防范 目录 实验目标 实验内容 实验环境 基础问题回答 预备知识 实验步骤 1 利用setoolkit建立冒名网站 ...

  4. docker 12 docker容器数据卷

    数据卷概念 ♣我们知道,当我们把一个运行中的容器关闭后,容器里的数据就没有了(如果你做了docker commit操作,数据会保留到新的镜像里面).所以我们就需要用容器数据卷把容器数据进行持久化储存. ...

  5. PostgreSQL安装和使用

    青岛OJ系统用的关系型数据库是PostgreSQL,为此对PostgreSQL大致了解下. 今天的主要话题围绕下面两个方面: PostgreSQL安装 PostgreSQL使用 一.PostgreSQ ...

  6. autoware

    在 Autoware目录下执行 git checkout 将版本切换到1.10.0 因为最新版驱动有问题然后执行 sudo apt-get update sudo apt-get install -y ...

  7. bat性能效率受啥影响

    代码效率的提升往往由算法决定,曾发过专贴(浅谈提高代码效率的编写习惯:http://tieba.baidu.com/p/1187281687),但是以实例为主,并没有太多的文字说明,现在归纳一下:影响 ...

  8. 使用ssh登录kali

    SSH为Secure Shell的缩写,SSH为建立在应用层基础上的安全协议.SSH是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议.利用SSH协议可以有效防止远程管理过程中的信息泄露问题 ...

  9. 多个jdk 变更 引起 tomcat插件 启动不了 The JRE could not be found.Edit the server and change the JRE location.

    The JRE could not be found.Edit the server and change the JRE location. 在Windows->Preferences-> ...

  10. 广师大python学习笔记求派的值

    用python语言算π值并且带有进度条 用python算圆周率π 1.准备第三方库pip 2.利用马青公式求π 3.用python语言编写出求圆周率到任意位的程序如下: from math impor ...