Android - 基于Toolbar的Navigation Drawer(Material Design)

搬运于博客园:http://www.cnblogs.com/Syun0929/p/4293085.html
介绍下基于Toolbar的Navigation Drawer

image.png

实现过程

1. 在 activity_asdemo.xml 中加入 DrawerLayout

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mydrawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ASDemoActivity" >
 
    <RelativeLayout
        android:id="@+id/view_base_bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
 
        <include layout="@layout/toolbar" />
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/mytoolbar"
            android:layout_margin="20dp"
            android:background="@android:color/black"
            android:padding="10dp"
            android:text="@string/hello_world" />
    </RelativeLayout>
 
    <!-- Sid Drawer -->
 
    <LinearLayout
        android:id="@+id/drawer_view"
        android:layout_width="270dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#6699CC00"
        android:clickable="true"
        android:fitsSystemWindows="true"
        android:orientation="vertical" >
    </LinearLayout>
 
</android.support.v4.widget.DrawerLayout>

2.toolbar.xml代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
 
    android:id="@+id/mytoolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="10dp"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize" >
 
</android.support.v7.widget.Toolbar>

3.在ASDemoActivity.java中添加DrawerLayout,部分代码如下:

private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
       ……
 
        // 打開 up button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 
        mDrawerLayout = (DrawerLayout) findViewById(R.id.mydrawer);
        //drawer toggle 放入 toolbar
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                mToolbar, R.string.drawer_open, R.string.drawer_close);
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

完成后,就会实现下面的交互效果。
  完整代码:https://github.com/Syun0929/MyASDemo
最终实现的交互效果:


一些需要注意的细节

1.有一些需要注意的地方需要补充下,目前实现了的效果中,DrawerLayout是覆盖status bar的,这样的好处是DrawerLayou有了更多的空间来展现内容。这个也是google官方推荐的效果,google官方的app也在逐步替换成这种效果。

  

  

  2.设置statusBar颜色
  A. 在 root 层的 drawer layout 跟 side drawer 的 layout 各別加上android:fitsSystemWindows="true" 属性;

<android.support.v4.widget.DrawerLayout
    ……
    android:fitsSystemWindows="true"
    ……>
 
    <!-- Content -->
    ……
 
    <!-- Sid Drawer -->
 
    <LinearLayout
      ……
        android:fitsSystemWindows="true"
       …… >
    </LinearLayout>
 
</android.support.v4.widget.DrawerLayout>

B. 在 v21/styles.xml中给android:statusBarColor属性添加一个带透明度的颜色值。

<style name="AppTheme" parent="AppTheme.Base">
      <!-- All customizations that are NOT specific to a particular API-level can go here. -->
      <item name="android:windowDrawsSystemBarBackgrounds">true</item>
      <!-- Status bar color -->
      <item name="android:statusBarColor">#88D81B60</item>
 
      <!-- Navigation bar color -->
      <item name="android:navigationBarColor">#00000000</item>
  </style>

3.如果想让DrawerLayout的区域只在toolbar下方,就要在xml中把toolbar放到DrawerLayout之外,效果图就不放了,代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity">
 
  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    ... />
 
  <android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_below="@+id/toolbar">
 
    <!-- Content -->
    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">
 
      ...
 
    </RelativeLayout>
 
    <!-- Side Drawer -->
    <LinearLayout
      android:id="@+id/drawer_view"
      android:layout_width="@dimen/navdrawer_width"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:background="#88FFFFFF"
      android:orientation="vertical">
 
    </LinearLayout>
 
  </android.support.v4.widget.DrawerLayout>
 
</RelativeLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,663评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,585评论 2 45
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,911评论 22 665
  • 每当我特别特别想回头时 就逼自己想起 曾经因为你 而变得面目全非的自己 然后 摁下心头的悸动
    南珩阅读 141评论 0 1
  • 你的归宿 是你自己 不凌驾他人 不委屈自己 不迂腐世故 丰盛 美好 灵动 自由地思索 孤独地繁茂 温柔地期盼 微笑...
    阳光精灵阅读 176评论 0 0