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

來源:互聯網
上載者:User

標籤:

  Material Design已經推出許久,有許多app都已經跟進了,這裡也介紹下基於Toolbar的Navigation Drawer是如何?的。

  • 樣式

  

  • 實現過程

  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);    }

  完成後,就會實現下面的互動效果。

  • 最終實現的互動效果:

  

  • 一些需要注意的細節

  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 - 基於Toolbar的Navigation Drawer(Material Design)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.