Material design has been introduced for a long time, there are many apps have been followed up, here also introduced under the toolbar-based navigation drawer is how to achieve.
1. Add Drawerlayout to Activity_asdemo.xml
<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:l ayout_height= "Match_parent" android:fitssystemwindows= "true" tools:context= ". Asdemoactivity "> <relativelayout android:id=" @+id/view_base_bottom "android:layout_width=" Match_pa Rent "android:layout_height=" match_parent "> <include layout=" @layout/toolbar "/> <textvi EW android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layou t_below= "@+id/mytoolbar" android:layout_margin= "20DP" android:background= "@android: Color/black" android:padding= "10DP" android:text= "@string/hello_world"/> </RelativeLayout> <!--Si D 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:or ientation= "Vertical" > </LinearLayout></android.support.v4.widget.DrawerLayout>
2.toolbar.xml Code
<?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. Add Drawerlayout to Asdemoactivity.java and some of the code is as follows:
Private Drawerlayout mdrawerlayout; Private Actionbardrawertoggle Mdrawertoggle; @Override protected void onCreate (Bundle savedinstancestate) { ... Open the Up button Getsupportactionbar (). Setdisplayhomeasupenabled (true); Mdrawerlayout = (drawerlayout) Findviewbyid (r.id.mydrawer); Drawer Toggle put toolbar mdrawertoggle = new Actionbardrawertoggle (this, mdrawerlayout, Mtoolbar, R.string.drawer_open, r.string.drawer_close); Mdrawertoggle.syncstate (); Mdrawerlayout.setdrawerlistener (Mdrawertoggle); }
When you are done, you will achieve the following interaction effect.
- The resulting interactive effect:
- Some details to be aware of
1. There are a number of areas needing attention that need to be added, the effect currently achieved, drawerlayout is covered by the status bar , the benefit is that drawerlayou has more space to present the content. This is Google's official recommendation of the effect, Google's official app is also gradually replaced by this effect.
2. Set StatusBar color
A. Add the android:fitssystemwindows= "true" attribute to the drawer layout of the root layer and the layout of the side drawer;
<android.support.v4.widget.drawerlayout ... Android:fitssystemwindows= "true" ......> <!--Content --... <!--Sid Drawer--- <linearlayout ... Android:fitssystemwindows= "true" ... > </linearlayout></ Android.support.v4.widget.drawerlayout>
B. Add a color value with transparency to the Android:statusbarcolor property in V21/styles.xml.
<style name= "Apptheme" parent= "Apptheme.base" > <!--All customizations that is 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. If you want the Drawerlayout area to be toolbar below the :
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "HTTP://SCHEMAS.A Ndroid.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 . Widgets. 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:layou t_height= "Match_parent" android:layout_gravity= "Start" android:background= "#88FFFFFF" Android: orientation= "Vertical" > </LinearLayout> </android.support.v4.widget.drawerlayout></ Relativelayout>
Android-Navigation Drawer based on toolbar (Material Design)