"Android Notes" drawer-style layout----drawerlayout

Source: Internet
Author: User

The effect is as follows:

Drawerlayout comes from the SUPPORT.V4 package, so do not consider compatibility issues. Second, this layout is similar to the rage side-by-side menu, but much lighter than the slide menu. The following describes how this layout is used.1. Import the SUPPORT.V4 package in your project. 2. Edit a layout, the root node is android.support.v4.widget.DrawerLayout, only two child nodes are allowed under this node, the first one is the content of the future main page, the second node is the "drawer" content, usually a ListView. Like what:
<android.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" > <LinearLay Out android:id= "@+id/content_frame" android:layout_width= "match_parent" android:layout_height= "Match_            Parent "android:orientation=" vertical "> <textview android:layout_width=" match_parent " android:layout_height= "wrap_content" android:text= "hahaha ha ha ha ha ha ha haha haha" android:textcolor= "#000 "Android:textsize=" 20sp "/> <button android:id=" @+id/but_open "Android:layo Ut_width= "Match_parent" android:layout_height= "wrap_content" android:text= "open drawer"/> </linea Rlayout> <!--Be sure to add layout_gravity= "Start"--<listview android:id= "@+id/left_drawer" Android       Oid:layout_width= "240DP" android:layout_height= "Match_parent" android:layout_gravity= "Start" android:background= "#111" Androi D:choicemode= "Singlechoice" android:divider= "@android: Color/transparent" android:dividerheight= "0DP"/&GT;&L T;/android.support.v4.widget.drawerlayout>
3. Write code to listen for related events:
Package Com.example.drawerlayoutdemo1;import Android.app.activity;import Android.os.bundle;import Android.support.v4.widget.drawerlayout;import Android.util.log;import Android.view.gravity;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.Button; Import Android.widget.listview;public class Mainactivity extends activity{protected static final String TAG = "Mainactivi  Ty ";p rivate drawerlayout mdrawerlayout = null;private ListView LV = null;private Button but_open = null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawer_layout); LV = (ListView) Findviewbyid (r.id.left_drawer) ; but_open = (Button) Findviewbyid (R.id.but_open); But_open.setonclicklistener (new Onclicklistener () {@Overridepublic void OnclIck (View v) {mdrawerlayout.opendrawer (gravity.left);}}); Lv.setadapter (New arrayadapter<string> (this,r.layout.test,r.id.tv_item,new string[]{"Entry 1", "Entry 2", "Entry 3"}); Mdrawerlayout.setdrawerlistener (New Drawerlayout.simpledrawerlistener () {@Overridepublic void ondrawerclosed (View Drawerview) {log.i (TAG, "drawer closed ..."); @Overridepublic void ondraweropened (View drawerview) {log.i (TAG, "drawer open ..."); @Overridepublic void Ondrawerslide (View drawerview, float slideoffset) {log.i (TAG, "drawer sliding ..."); @Overridepublic void ondrawerstatechanged (int newstate) {switch (newstate) {case DRAWERLAYOUT.STATE_DRAGGING:LOG.I ( Tag, "drag state"); Break;case drawerlayout.state_idle:log.i (tag, "static State"); Break;case drawerlayout.state_settling:log.i ( TAG, "set state"); break;default:break;}}); Lv.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, View view,int position, long ID) {switch (position) {case 0:LOG.I (tag, "Entry 1 is clicked"); Break;case 1:log.i (Tag, "Entry 2 is clicked"); Case 2:LOG.I (TAG, "Entry 3 is clicked"), break;}});}} 










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: info-contact@alibabacloud.com 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.