標籤:
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)