添加抽屉导航

Android Material风格的应用(一)--AppBar TabLayout
Android Material风格的应用(二)--RecyclerView
Android Material风格的应用(三)--DrawerLayout
Android Material风格的应用(四)--FloatActionButton
Android Material风格的应用(五)--CollapsingToolbar

DrawerLayout

工程源码

  • 创建导航菜单
    res/menu/menu-natigation.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
    <item android:icon="@drawable/ic_home_black_24dp"
    android:tint="@color/button_grey"
    android:title="One" />
    <item android:icon="@drawable/ic_favorite_black_24dp"
    android:tint="@color/button_grey"
    android:title="Two"/>
    <item android:icon="@drawable/ic_bookmark_border_black_24dp"
    android:tint="@color/button_grey"
    android:title="Three"/>
    </group>
    </menu>

    创建导航的布局 res/layout/navigation.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="@dimen/navheader_height"
    android:background="?attr/colorPrimaryDark"
    android:padding="@dimen/md_keylines">
    </LinearLayout>

    activity_main.xml中加入DrawerLayout ,是DrawerLayout作为对顶层,使CoordinatorLayout作为它的
    子级

    <android.support.v4.widget.DrawerLayout
    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:fitsSystemWindows="true">
    ......
    </android.support.v4.widget.DrawerLayout>

    CoordinatorLayout的同一级添加NavigationView

    <android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/nav_view"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/menu_navigation">

    MainActivity.javaonCreate中添加相关的逻辑

    NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
    ActionBar supportActionBar = getSupportActionBar();
    if(supportActionBar != null){
    supportActionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
    supportActionBar.setDisplayHomeAsUpEnabled(true);
    }
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    item.setChecked(true);
    mDrawerLayout.closeDrawers();;
    return true;
    }
    });

    相应左上角的menu,打开导航

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id == android.R.id.home){
    mDrawerLayout.openDrawer(GravityCompat.START);
    }
    return super.onOptionsItemSelected(item);
    }

Android Material风格的应用(三)--DrawerLayout的更多相关文章

  1. Android Material风格的应用(五)--CollapsingToolbar

    Collapsing Toolbar Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewA ...

  2. Android Material风格的应用(四)--FloatActionButton

    添加 FloatActionButton和SnackBar Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--Re ...

  3. Android Material风格的应用(二)--RecyclerView

    添加RecyclerView Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewAndro ...

  4. Android Material风格的应用(一)--AppBar TabLayout

    打造Material风格的Android应用 Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerV ...

  5. Android Metro风格的Launcher开发系列第三篇

    前言: 各位小伙伴,又到了每周更新文章了时候了,本来是周日能发出来呢,这不是赶上清明节吗,女王大人发话了,清明节前两天半陪她玩,只留给我周一下午半天时间写博客,哪里有女王哪里就有压迫呀有木有!好了闲话 ...

  6. Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果

    前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...

  7. Android Material Design之Toolbar与Palette

    转:http://blog.csdn.net/jdsjlzx/article/details/41441083 前言 我们都知道Marterial Design是Google推出的全新UI设计规范,如 ...

  8. android Material Design详解

    原文地址:http://blog.csdn.net/jdsjlzx/article/details/41441083/ 前言 我们都知道Marterial Design是Google推出的全新UI设计 ...

  9. Android Material Design 兼容库的使用

    Android Material Design 兼容库的使用 mecury 前言:近来学习了Android Material Design 兼容库,为了把这个弄懂,才有了这篇博客,这里先推荐两篇博客: ...

随机推荐

  1. pip版本及升级 pip安装指定模板

    昨天在微信聊天,一妹子9点的时候告诉我她要看书了,让明天聊,瞬间自己心中那颗学习的种子燃烧起来,思来想去还是继续学习自己之前未学好的python吧,因为之前有了点点的python基础,所以本次打算从p ...

  2. ItelliJ IDEA开发工具使用—创建一个web项目(转)

    最近想用IDEA编辑器开发,但是平时都用MyEclipse和eclipse习惯了,突然间用IDEA到处碰壁的感觉.在不断的摸索之后终于苦尽甘来,学会了基本的web程序如何创建以及运行了.期间在网上找了 ...

  3. C# 操作Excel常见错误

    1.未在本地注册 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + &qu ...

  4. 智课雅思词汇---三、aud和auto和bene是什么意思

    智课雅思词汇---三.aud和auto和bene是什么意思 一.总结 一句话总结:aud:听 auto:自己,self bene:good,well 1.anthropo是什么意思? anthropo ...

  5. powershell《语音报警系统》

    用powershell实现:“倩女幽魂姥姥”版<语音报警系统> ------[第一章 前言]------ win7,及以上版本中,是自带语音库的,系统自带一套女声中文库,一套女声英文库.用 ...

  6. express中的中间件理解

    什么是中间件 中间件是一个可访问请求对象(req)和响应对象(res)的函数,在 Express 应用的请求-响应循环里,下一个内联的中间件通常用变量 next 表示.中间件的功能包括: 执行任何代码 ...

  7. Logistic Regression and Newton's Method

    Data For this exercise, suppose that a high school has a dataset representing 40 students who were a ...

  8. 【转】BAT取当前日期的前一天

    ============================取前一天的日期       OK============================@echo off ::前一天的日期,格式化输出echo ...

  9. DG的数据保护模式

    DG的数据保护模式 数据保护膜有三种: – Maximum protection – Maximum availability – Maximum performance Maximum protec ...

  10. visualSVN+花生壳实现外网访问局域网内SVN

    使用SubVersion+TortoiseSVN局域网内访问SVN成功后,想从外网访问SVN,使用花生壳绑定路由器动态DNS,但是折腾半天没搞定,突然发现一个帖子http://hi.baidu.com ...