本文将介绍使用Google最新推出规范式设计中的NavigationView和DrawerLayout结合实现侧滑菜单栏效果,NavigationView是android-support-design包下的一个控件,该包下还有AppBarLayout、CoordinatorLayout、FloatingActionButton、SnackBar、TabLayout、TextInputLayout等控件,也是Google在Android
5.x推荐规范式使用的控件。本系列将逐一介绍每个控件的使用。。。

好了,先来看看本文最终的效果图吧!

它把标题栏也遮住了,正是符号Google的材料设计思想。。。

现在我们分几步说说怎么实现这个效果吧:

一、首先就需要添加包依赖,废话就不说了,上图

二、主布局:activity_main.xml

<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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#30469b"/>
        <!--内容显示布局-->
        <FrameLayout
            android:id="@+id/frame_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>

其中在NavigationView布局中,需要给NavigationView设置app:headerLayout和app:menu,那是什么意思呢?

  • headerLayout就是给导航栏增加一个头部Layout。
  • menu就是对应菜单项的选择条目。

在NavigationView中还有一些比较重要的属性,如下:

  • itemBackground — 设置菜单项的背景
  • itemIconTint — 设置菜单项的图标的着色
  • itemTextColor — 这只菜单项目的文本颜色

三、分别写headerLayout和menu对应的布局navigation_header.xml和菜单drawer.xml文件

navigation_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="200dp"
    android:background="#30469b">
    <TextView
        android:id="@+id/header_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="HeaderLayout"
        android:textColor="#ffffff"
        android:textSize="25sp" />
</RelativeLayout>

drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/item_one"
            android:icon="@mipmap/icon"
            android:title="我的动态"></item>
        <item
            android:id="@+id/item_two"
            android:icon="@mipmap/icon"
            android:title="我的留言"></item>
        <item
            android:id="@+id/item_three"
            android:icon="@mipmap/icon"
            android:title="附近的人"></item>
    </group>
</menu>

比如我们需要添加一个分组菜单,即二级菜单,如:

那么menu布局可以这样:(item的点击事件还是和之前的一样)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/item1"
            android:title="分组一">
            <menu>
                <group android:checkableBehavior="single">
                    <item
                        android:id="@+id/item_one"
                        android:icon="@mipmap/icon"
                        android:title="我的动态"></item>
                    <item
                        android:id="@+id/item_two"
                        android:icon="@mipmap/icon"
                        android:title="我的留言"></item>
                    <item
                        android:id="@+id/item_three"
                        android:icon="@mipmap/icon"
                        android:title="附近的人"></item>
                </group>
            </menu>
        </item>
        <item
            android:id="@+id/item2"
            android:title="分组二">
            <menu>
                <group android:checkableBehavior="single">
                    <item
                        android:id="@+id/item2_one"
                        android:icon="@mipmap/icon"
                        android:title="我的动态"></item>
                    <item
                        android:id="@+id/item2_two"
                        android:icon="@mipmap/icon"
                        android:title="我的留言"></item>
                    <item
                        android:id="@+id/item2_three"
                        android:icon="@mipmap/icon"
                        android:title="附近的人"></item>
                </group>
            </menu>
        </item>
    </group>
</menu>

四、这时候开始写代码了,也很简单:

MainActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置ToolBar
        final Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(mToolbar);

        //设置抽屉DrawerLayout
        final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
                R.string.drawer_open, R.string.drawer_close);
        mDrawerToggle.syncState();//初始化状态
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        //设置导航栏NavigationView的点击事件
        NavigationView mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId())
                {
                    case R.id.item_one:
                        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentOne()).commit();
                        mToolbar.setTitle("我的动态");
                        break;
                    case R.id.item_two:
                        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentTwo()).commit();
                        mToolbar.setTitle("我的留言");
                        break;
                    case R.id.item_three:
                        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new FragmentThree()).commit();
                        mToolbar.setTitle("附近的人");
                        break;
                }
                menuItem.setChecked(true);//点击了把它设为选中状态
                mDrawerLayout.closeDrawers();//关闭抽屉
                return true;
            }
        });
    }
}

好了,做完上面四步,恭喜,成功了!!!喜欢点个赞吧。。。

源码地址:http://download.csdn.net/detail/u010687392/8890505

Material Design之NavigationView和DrawerLayout实现侧滑菜单栏的更多相关文章

  1. Android Material Design之 NavigationView侧滑界面自定义 随笔

    一.侧滑界面Menu自定义: 在menu文件夹下新建activity_main_drawer.xml文件,自定义标题和icon: <?xml version="1.0" en ...

  2. Material Design: NavigationView FlaotingActionBar SnackBar采用

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

  3. 安卓开发笔记(三十四):Material Design框架实现优美的左侧侧滑栏

    首先我们先上图:  下面是主页面的代码,activity_main.xml: <?xml version="1.0" encoding="utf-8"?& ...

  4. Android开发实战之拥有Material Design风格的侧滑布局

    在实现开发要求中,有需要会使用抽屉式布局,类似于QQ5.0的侧滑菜单,实现的方式有很多种,可以自定义控件,也可以使用第三方开源库. 同样的谷歌也推出了自己的侧滑组件——DrawLayout,使用方式也 ...

  5. 安卓Design包之NavigationView结合DrawerLayout,toolbar的使用,FloatingActionButton

    注意:使用前需要添加Design依赖包,使用toolbar时需要隐藏标题头 FloatingActionButton 悬浮按钮:FloatingActionButton是重写ImageView的,所有 ...

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

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

  7. Material Design 组件之NavigationView

    今天来看一下 NavigationView 的使用,NavigationView 是一个标准的导航菜单,其菜单内容由菜单资源文件来填充,NavigationView 一般和 DrawerLayout ...

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

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

  9. Android Material Design控件学习(二)——NavigationView的学习和使用

    前言 上次我们学习了TabLayout的用法,今天我们继续学习MaterialDesign(简称MD)控件--NavigationView. 正如其名,NavigationView,导航View.一般 ...

随机推荐

  1. 《Non-Negative Matrix Factorization for Polyphonic Music Transcription》译文

    NMF(非负矩阵分解),由于其分解出的矩阵是非负的,在一些实际问题中具有非常好的解释,因此用途很广.在此,我给大家介绍一下NMF在多声部音乐中的应用.要翻译的论文是利用NMF转录多声部音乐的开山之作, ...

  2. Python动态展现之一

    首先: def f(): print('first') def g(): f() g() def f(): print('second') g() 结果: >>> first sec ...

  3. Redis 学习笔记4: Redis 3.2.1 集群搭建

    在CenOS 6.7 linux环境下搭建Redis 集群环境 1.下载最新的Redis版本 本人下载的Redis版本是3.2.1版本,下载之后,解压,编译(make): 具体操作可以参考我的博文:R ...

  4. 如何正确使用const、static、extern

    转自:http://www.jianshu.com/p/2fd58ed2cf55 前言 本篇文章主要介绍在开发中怎么使用const.static.extern关键字. 一.const 与宏的区别: c ...

  5. tomcat自动运行磁盘任意位置上的项目、使用Maven对tomcat进行自动部署

     对于非Maven的web项目,有时候我们想不时常通过打war包.拷贝war包.启动tomcat来运行项目.这时候我们可以通过以下方式来进行配置: 1.1:创建web工程.工程结构如下: 1.2. ...

  6. Java并发框架——什么是AQS框架

    什么是AQS框架 1995年sun公司发布了第一个java语言版本,可以说从jdk1.1到jdk1.4期间java的使用主要是在移动应用和中小型企业应用中,在此类领域中基本不用设计大型并发场景,当然也 ...

  7. Java 拓展之调用其他语言

    目前而言,编程语言真的是太多了.每一种都是一种智慧的结晶,但是每个都存在其缺点.网上经常能看到一些程序员争论"XX是世界上最好的语言"之类的话题.其实我们大可不必这样.语言本身只是 ...

  8. maven的UnsupportedClassVersionError

    问题描述 我安装了maven3.3.3,配置好了M2_HOME和path环境变量之后,执行mvn -v报错:java.lang.UnsupportedClassVersionError: -. : U ...

  9. unity使用ugui自制调色面板

    突然想实现一个调色面板,然后开工... 首先找找有没有什么接口可调,木有找到,找到一些调用win32实现的本地颜色面板的调用,感觉不科学,反正多平台肯定是搞不定的. 既然没找到,还是老老实实的自己写吧 ...

  10. Android对话框AlertDialog-android学习之旅(四十二)

    对话框简介 android提供了丰富的对话框支持,支持四种如下的对话框. AlertDialog简介 介绍上面六个方法的代码示例 setMessage() <?xml version=" ...