Drawerlayout has been out for a long time, personally feel that the domestic app are deeply poisoned by iOS in the design are scrambling to imitate the style of iOS, have forgotten what is the unique Android style, they have to learn the first and then with the product in the project to use a series of Android style, It happened that drawerlayout menu navigation was won by me and was used on the project.
First, drawerlayout exists in the V4 package, and we just need to configure it in XML
<android. Support. V4. Widgets. DrawerlayoutAndroid:id="@+id/drawerlayout"Xmlns:android="Http://schemas.android.com/apk/res/android"Android:layout_width="Match_parent"android:layout_height="Match_parent"> <textview android:layout_width="Match_parent"android:layout_height="Match_parent"android:text="@string/hello_world"Android:textcolor="@android: Color/black"android:gravity="Center"/> <linearlayout android:layout_width="Match_parent"android:layout_height="Match_parent"android:layout_gravity="Start"Android:background="#ffffff"android:orientation="Vertical"/></android. Support. V4. Widgets. Drawerlayout>
In this need to introduce the next Actionbardrawertoggle, control drawerlayout Display/hide use.
Use it in the two callback functions in the activity:
Onconfigurationchanged
onoptionsitemselected
Call Actionbardrawertoggle.syncstate () in the activity's onpostcreate (), indicating that the actionbardrawertoggle is synchronized with the Drawerlayout State, and set the drawer icon in the Actionbardrawertoggle to Actionbar's Home-button
Public class draweractivity extends appcompatactivity {Drawerlayout mdrawerlayout; Actionbardrawertoggle Toggle;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.test_activity); Mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawerlayout); Toggle =NewActionbardrawertoggle (draweractivity. This, Mdrawerlayout, R.string.hello_world, R.string.hello_world); Mdrawerlayout.setdrawerlistener (toggle); Getsupportactionbar (). sethomebuttonenabled (true); Getsupportactionbar (). setdisplayhomeasupenabled (true); Getsupportactionbar (). Settitle ("Testdrawerlayout"); }@Override Public void onpostcreate(Bundle savedinstancestate) {Super. Onpostcreate (Savedinstancestate); Toggle.syncstate (); }@Override Public void onconfigurationchanged(Configuration newconfig) {Super. onconfigurationchanged (Newconfig); Toggle.onconfigurationchanged (Newconfig); }@Override Public Boolean onoptionsitemselected(MenuItem Item) {if(toggle.onoptionsitemselected (item))return true;return Super. onoptionsitemselected (item); }}
The above steps set up to successfully use Drawerlayout in your project, but there are a few things to note:
1.android.support.v4.widget.drawerlayout is present under the V4 package
2. Themes need to show actionbar, such as Theme.AppCompat.Light.DarkActionBar
3.ActionBarDrawerToggle is a drawerlistener implementation, we can write our own drawerlistener to achieve the specific requirements.
We can find the unique Android style that we use is easy, but we are much affected by iOS.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android-drawerlayout Introduction