Android Drawer Layout

Source: Internet
Author: User

At present, some apps use a kind of drawer-like layout, like QQ kind, feel very flashy, I have always wanted to do a layout like that, (PS online A lot of such examples, I do the following is to refer to the changes on the Internet)

No more nonsense, just on the code.

1, first set up a layout file Activity_drawer.xml

1<relativelayout xmlns:android= "Http://"2Xmlns:tools= "Http://"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9Tools:context= ". Draweractivity ">Ten    <<!--Focus here -- OneAndroid:id= "@+id/drawer_layout" AAndroid:layout_width= "Match_parent" -android:layout_height= "Match_parent" > -<Framelayout theAndroid:id= "@+id/content_frame" -Android:layout_width= "Match_parent" -android:layout_height= "Match_parent" > -<Button +Android:id= "@+id/btn" -Android:layout_width= "Wrap_content" +android:layout_height= "Wrap_content" Aandroid:text= "Open" atAndroid:layout_alignparenttop= "true" -Android:layout_alignparentleft= "true" -Android:layout_alignparentstart= "true" -Android:layout_alignparentright= "true" -Android:layout_alignparentend= "true"/> -</FrameLayout> in<ListView -Android:id= "@+id/left_drawer" toAndroid:layout_width= "200DP" +android:layout_height= "Match_parent" -Android:layout_gravity= "Start" theAndroid:background= "#ff349710" *Android:choicemode= "Singlechoice" $android:dividerheight= "0DP"Panax Notoginsengandroid:layout_margintop= "30DP" -/> the</> +</RelativeLayout>

2. The corresponding activity file

1  Public classDraweractivityextendsActivity {2      Public Static Finalstring[] str =Newstring[]{"East", "West", "South", "North", "middle",3"Southeast", "Northwest", "Southwest", "Northeast", "Midwest", "Central South", "Central North"};4     PrivateDrawerlayout mdrawerlayout =NULL;5     PrivateListView Mlistview;6 @Override7     protected voidonCreate (Bundle savedinstancestate) {8         Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_drawer);Ten  OneMdrawerlayout =(drawerlayout) Findviewbyid (; AMlistview =(ListView) Findviewbyid (; -  -          the         //The following two ways are possible, the latter to create more than one value of the array file -Mlistview.setadapter (NewArrayadapter<string> ( This, Android. R.LAYOUT.SIMPLE_LIST_ITEM_1,STR)); -         //Mlistview.setadapter (New arrayadapter<string> (this,android. R.layout.simple_list_item_1,getresources (). Getstringarray (R.array.planets_array)); -Button Button =(Button) Findviewbyid (R.ID.BTN); +Button.setonclicklistener (NewView.onclicklistener () { -  + @Override A              Public voidOnClick (View v) { at                 //Press the button to open the drawer - Mdrawerlayout.opendrawer (gravity.left); -                 //mdrawerlayout.closedrawers ();//can not listen, the default click other places can be closed -                 //adapter.notifydatasetchanged (); -             } -         }); in         //List Control Listener -Mlistview.setonitemclicklistener (NewDraweritemclicklistener ()); to     } +  -     Private classDraweritemclicklistenerImplementsListview.onitemclicklistener { the @Override *          Public voidOnitemclick (adapterview Parent, view view,intPositionLongID) { $ SelectItem (position);Panax Notoginseng         } -     } the     //Simple output Some information +     Private voidSelectItem (intposition) { A         NewAlertdialog.builder ( This) the. Settitle ("open") +. Setmessage ("Open position:" +position+ "\ n Location value is:" +Str[position]) -. Setpositivebutton ("OK",NewDialoginterface.onclicklistener () { $ @Override $                      Public voidOnClick (Dialoginterface Dialoginterface,inti) { -                     //Monitoring Events -                     } the }). Show (); -     }Wuyi}

3. The simple effect is as follows:

This is just the effect of achieving, about why I have studied thoroughly before sharing.

Android Drawer Layout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.