Drawerlayout is a layout control in the Android V4 package, used to implement a drawer style layout.
Drawerlayout by setting the layout_gravity of the child view to determine which side of the screen the Child view is docked on, waiting for the user to drag it in or click the button to pull the drawer open.
The following is a simple drawerlayout of the code in the layout file:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <Android.support.v7.widget.ToolbarAndroid:id= "@+id/toolbar"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@color/colorprimary"Android:minheight= "? Attr/actionbarsize" /> <Android.support.v4.widget.DrawerLayoutAndroid:id= "@+id/drawer"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <ImageViewAndroid:id= "@+id/bg"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:contentdescription= "@string/app_name"Android:scaletype= "Centercrop"android:src= "@mipmap/bg4" /> <!--android:layout_gravity: Sets the position of the drawer layout, start or left, end or right - <ListViewAndroid:id= "@+id/drawer_list"Android:layout_width= "256.0dip"Android:layout_height= "Match_parent"android:layout_gravity= "Start"Android:background= "@color/colorprimary"Android:choicemode= "Singlechoice"Android:divider= "#00000000" /> </Android.support.v4.widget.DrawerLayout></LinearLayout>
In the code, we set the Layout_gravity property to start for the ListView, which means we add the ListView to the drawer and hide it on the left side of the screen. If we drag the finger to the right on the left edge of the screen, you can drag the drawer out.
We can open or close the drawer by drawerlayout the Opendrawer () and Closedrawer () methods of the object.
Next, let's introduce the methods used by Drawerlayout and toolbar.
Let's look at the effect of the operation:
As you can see, there is a button on the left-hand side of the toolbar that can follow the click Animation to change the look, then open the drawer. This button is an object called Actionbardrawertoggle, which can be pulled from the toolbar drawer by binding the object to the drawer. The specific code is as follows:
// Initialize toolbar Setsupportactionbar (toolbar); // Click the button to eject the drawer New Actionbardrawertoggle (mainactivity. This, drawer, toolbar, R.string.drawer_open, r.string.drawer_close); Drawertoggle.syncstate (); Drawer.adddrawerlistener (drawertoggle);
The ListView data adapter and the item's click event in the ListView are no longer mentioned.
The above is the basic usage of drawerlayout Introduction, the following sticker code cloud on the source, for your reference.
Demo Address
Drawerlayout use of "android-v"