NavigationView可以实现美观的菜单功能展示,下面看一下怎么使用NavigationView

先是主Activity

activity_main.xml:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" /> <android.support.v4.widget.DrawerLayout
android:id="@+id/drawer1"
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> </android.support.constraint.ConstraintLayout> <android.support.design.widget.NavigationView
android:id="@+id/navMenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/main_header"
app:menu="@menu/menu1">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout> </LinearLayout>

这个布局是这样的:

最外层是一个上下结构的LinearLayout,从上到下依次是一个Toolbar、一个DrawerLayout

DrawerLayout包括一个ConstraintLayout主界面和一个NavigationView抽屉界面

NavigationView导航菜单包括一个头部,以及头部以下的菜单部分

然后需要添加NavigationView导航菜单的头部,以及菜单:

头部main_header.xml:

<?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"> <ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_weight="1"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</LinearLayout>

这个头部只有一个ImageView,我先不为这个ImageView指定图片资源,待会我动态指定资源,以便于将这个图片显示成圆形的

导航菜单menu1.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single">
<item
android:id="@+id/editItem"
android:icon="@android:drawable/ic_menu_edit"
android:title="编辑"
app:showAsAction="always" />
<item
android:id="@+id/helpItem"
android:icon="@android:drawable/ic_menu_help"
android:title="帮助"
app:showAsAction="always" />
<item
android:id="@+id/deleteItem"
android:icon="@android:drawable/ic_menu_delete"
android:title="删除"
app:showAsAction="always" />
</group>
</menu>

这个菜单是有一个group菜单组,里面嵌套了三个item菜单项,注意group组有一个重要的属性android:checkableBehavior="single",这个一定要添加,不然到时候选中效果显示不出来。

最后就是主Activity的java类了

MainActivity.java:

 package com.example.chenrui.app1;

 import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); final DrawerLayout drawerLayout = findViewById(R.id.drawer1);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
} @Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
toggle.syncState();
drawerLayout.addDrawerListener(toggle); final NavigationView navMenu = findViewById(R.id.navMenu);
navMenu.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
}); ImageView imageView = navMenu.getHeaderView(0).findViewById(R.id.imageView1);
RoundedBitmapDrawable bitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),BitmapFactory.decodeResource(getResources(),R.drawable.img01));
bitmapDrawable.setCircular(true);
imageView.setImageDrawable(bitmapDrawable);
}
}

第24到42行代码是指定Activity的菜单栏,并且为菜单栏添加切换抽屉界面显示或隐藏的按钮

第44到52行代码是响应NavigationView选择事件的,选择一个菜单项,会把对应菜单项置为选中状态,并且隐藏抽屉界面,这个里面还应该有菜单要执行的其他动作,我这里是示例,所以没有做别的操作

第54到57行代码是为NavigationView的头部图片动态指定一个图片资源,并且把图片改成圆角的,注意第54行代码查找图片的方式,因为图片位于NavigationView组件的headerLayout中,无法直接通过findViewById找到这个图片,需要先找到NavigationView组件头部的View,然后通过头部的View找到图片组件。这里是使用了RoundedBitmapDrawable来实现圆角图片的,用起来还是比较简单的。

最后看一下执行的效果:

android中NavigationView(Design Support)的使用的更多相关文章

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

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

  2. Android学习之Design Support Library中TextInputLayout的使用

    今天学习了一个Android Design Support Library 中的TextInputLayout控件,感觉还不错,较之以往的Editetxt,多了几分灵活性,使用也非常easy,故此给大 ...

  3. Android Design Support Library(二)用NavigationView实现抽屉菜单界面

    NavigationView在MD设计中非常重要,之前Google也提出了使用DrawerLayout来实现导航抽屉.这次,在Android Design Support Library中,Googl ...

  4. Android Design Support Library初探,NavigationView实践

    前言 在前几天的IO大会上,Google带来了Android M,同时还有Android支持库的新一轮更新,其中更是增加一个全新的支持库Android Design Support Library,包 ...

  5. android中Snackbar(Design Support)的使用

    Snackbar是Android Design Support Library中的一个组件,想使用Snackbar,必须先引入Design Support,我这里引入的是当前的最新版本: implem ...

  6. Android Design Support Library使用详解

    Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...

  7. 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...

  8. 【转】Android的材料设计兼容库(Design Support Library)

    转自:http://www.jcodecraeer.com/a/anzhuokaifa/developer/2015/0531/2958.html?mType=Group Android的材料设计兼容 ...

  9. Android应用Design Support Library完全使用实例

    阅读目录 2-1 综述 2-2 TextInputLayout控件 2-3 FloatingActionButton控件 2-4 Snackbar控件 2-5 TabLayout控件 2-6 Navi ...

随机推荐

  1. error CS0234: 命名空间“XXX”中不存在类型或命名空间名称“UserInfoVm”(是否缺少程序集引用?)

    □ 背景 UserInfoVm是在MVC的Models文件夹中的一个view model,这个view model是某部分视图的的页面Model.当加载这个部分视图的时候报了错. □ 思考 UserI ...

  2. iOS中使用RegexKitLite来试用正则表达式

    转:http://blog.csdn.net/nullcn/article/details/6338592 准备工作,下载RegexKitLite 软件包,解压后有2个文件,需要加载到project中 ...

  3. 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏的代码

    在ipad.iphone网页开发中,我们很可能需要判断是横屏或者竖屏.下面就来介绍如何用 jQuery 判断iPad.iPhone.Android是横屏还是竖屏的方法 其实主要是通过window.or ...

  4. mysql error You must reset your password using ALTER USER statement before executing this statement.

    mysql修改密码Your password does not satisfy the current policy requirements 出现这个问题的原因是:密码过于简单.刚安装的mysql的 ...

  5. Mybatis对oracle数据库进行foreach批量插入操作

    MySQL支持的语法 INSERT INTO `tableX` ( `a`, `b`, `c`, `d`, `e` ) VALUES <foreach collection ="lis ...

  6. 通过图形化工具来画shape

    前两天一个哥们分享了十分好用的图形化工具,这样我们就能实时的看到自己用代码写出来的shape是怎么样的了,更牛的是它可以让我们自己去设定值,最后生成代码,这样我们为什么要去自己写shape呢?如果一个 ...

  7. TextView字体,行距,html格式,超链接,最大长度的设定

    颜色,大小 <!-- 设置字体的大小,推荐用sp做单位:字体颜色以#开头 --> <TextView android:id="@+id/textView1" an ...

  8. java的mock测试框架

    无论是敏捷开发.持续交付,还是测试驱动开发(TDD)都把单元测试作为实现的基石.随着这些先进的编程开发模式日益深入人心,单元测试如今显得越来越重要了.在敏捷开发.持续交付中要求单元测试一定要快(不能访 ...

  9. 统计学中RR OR AR HR的区别

    一.相对危险度(RR)——队列研究中分析暴露因素与发病的关联程度 队列研究是选择暴露及未暴露于某一因素的两组人群,追踪其各自的发病结局,比较两组发病结局的差异,从而判定暴露因素与疾病有无关联及关联大小 ...

  10. [leetcode]Edit Distance @ Python

    原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...