Preface, this article is the most basic use of drawerlayout to achieve the drawer effect, I also try to streamline to the most efficient code, I will post the other more complex features.
Look first.
The text in the title bar displays different styles depending on the item you clicked.
directly on the code.
Here is the code for Activity_main
<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 "> <andr Oid.support.v4.widget.DrawerLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+ Id/drawer_layout "android:layout_width=" match_parent "android:layout_height=" match_parent "> <! --The current main interface display content View-<!--The main content must be the first element of Drawerlayout, because it will be first displayed, and the drawer must be above the content-<framelayout Android:id= "@+id/content_frame" android:layout_width= "match_parent" android:layout_height= "Match_ Parent "/> <!--navigation drawer View--<listview android:id=" @+id/left_drawer "Android : layout_width= "240DP" android:layout_height= "match_parent" android:layout_gravity= "left" Android:background= "@android: Color/holo_red_dArk "Android:choicemode=" Singlechoice "android:divider=" @android: Color/transparent "Andro id:dividerheight= "5DP"/> </android.support.v4.widget.DrawerLayout></RelativeLayout>
- The width of the drawer menu is the same as the height of the
dp parent view. The width of the drawer menu should be no more than 320DP, so that users can see some content interface when the menu is open.
Next is the mainactivity code
Package Com.example.chouti;import Android.os.bundle;import Android.app.activity;import Android.content.res.configuration;import Android.view.menuitem;import Android.view.view;import Android.view.window;import Android.widget.adapterview;import Android.widget.AdapterView.OnItemClickListener; Import Android.widget.arrayadapter;import Android.widget.listview;import Android.support.v4.app.actionbardrawertoggle;import Android.support.v4.view.gravitycompat;import Android.support.v4.widget.drawerlayout;public class Mainactivity extends Activity {private string[] Mplanettitles = {" Hahhooo "," JDAHLDFJ "," Is this true "," This should be true "," 4 "," JDAHLDFJ "," Is this true "," This should be true "," JDAHLDFJ "," Is this true "," This should be true ";p rivate Drawerlayout mdrawerlayout;private actionbardrawertoggle mdrawertoggle;private ListView mDrawerList; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawer_layout);//Initialize ListView and adaptation data Initlistview (); Mdrawerlayout.setdrawershadow (R.drawable.ic_launcher, Gravitycompat.start); Initialize Actionbardrawertogglemdrawertoggle = new Actionbardrawertoggle (this,//the Activity object of the display drawer mdrawerlayout,// Drawerlayout object R.drawable.ic_launcher,//A drawable resource used to indicate the drawer (if you want to display this icon, you must override the Onpostcreate (Bundle Savedinstancestate) r.string.hello_world,//a text to describe the open drawer (to support accessibility) r.string.hello_world);//A text that describes the closing drawer ( to support Accessibility)//set Actionbardrawertoggle to Drawerlistenermdrawerlayout.setdrawerlistener (mdrawertoggle);// Actionbar operation mode on (call this method, click on the icon to respond) Getactionbar (). Setdisplayhomeasupenabled (True); Getactionbar (). Sethomebuttonenabled (TRUE);//Note: Getactionbar () is added at API level 11}//here is onpostcreate (Bundle savedinstancestate) method//@ override//protected void Onpostcreate (Bundle savedinstancestate) {////super.onpostcreate (savedinstancestate);// Mdrawertoggle.syncstate ();//}private void Initlistview () {mdrawerlist = (ListView) Findviewbyid (r.id.left_drawer);// Adaptation Data Mdrawerlist.setadapter (new arrayadapter< String> (this,android. R.layout.simple_list_item_1,mplanettitles));//Set the Monitor event Mdrawerlist.setonitemclicklistener for the ListView (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, View view,int position, long ID) {/ /update title, and close drawer settitle (mplanettitles[position]); Mdrawerlayout.closedrawer (mdrawerlist);});} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//pass event to Actionbardrawertoggle, and if true, it will handle the icon response event if ( Mdrawertoggle.onoptionsitemselected (item)) {return true;} Handle other actions click on the entry return super.onoptionsitemselected (item);}}
for Actionbar related knowledge, if you want to learn more, please click here http://blog.csdn.net/harryweasley/article/details/42027521For more detailed commentary on Drawerlayout, you can click herehttp://blog.csdn.net/harryweasley/article/details/42027487
orhttp://blog.csdn.net/harryweasley/article/details/42027563
The above three articles are reproduced by me, help me to complete the above project.
Use Drawerlayout to achieve simple drawer effect