android--Sliding Menu

Source: Internet
Author: User

Drawerlayout is a layout with two child controls, the first child control is what is displayed in the home screen, and the second child control is what is displayed in the slide menu:

1 <Android.support.v4.widget.DrawerLayout2     xmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:app= "Http://schemas.android.com/apk/res-auto"4 Android:id= "@+id/drawer_layout"5 Android:layout_width= "Match_parent"6 Android:layout_height= "Match_parent"7     >8 9     <FramelayoutTen         Android:layout_width= "Match_parent" One Android:layout_height= "Match_parent"> A  -         <Android.support.v7.widget.Toolbar -             Android:id= "@+id/toolbar" the Android:layout_width= "Match_parent" - Android:layout_height= "? Attr/actionbarsize" - Android:background= "? Attr/colorprimary" - Android:theme= "@style/themeoverlay.appcompat.dark.actionbar" + App:popuptheme= "@style/themeoverlay.appcompat.light"/> -  +     </Framelayout> A     <TextView at         Android:layout_width= "Match_parent" - Android:layout_height= "Match_parent" - android:layout_gravity= "Start" - Android:text= "This is a menu" - android:textsize= "30SP" - Android:background= "#FFF"/> in </Android.support.v4.widget.DrawerLayout>

Note that the Layout_gravity property of the second child control TextView Specifies whether the sliding menu is on the left or the right side of the screen, and the property value is in the same place, using the start expression to determine the system language.

The effect here is this:

Then in the title bar to add a navigation button, click the Navigation button can also open the sliding menu, the implementation principle is that the title bar to the left there is a button called Homeasup, its default icon is a return arrow, meaning is to return to the previous activity, so only need to show it out, Modify its icon and click events.

Specific Java code:

1  Public classMainactivityextendsappcompatactivity {2 3     Privatedrawerlayout mdrawerlayout;4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_main);8Toolbar Toolbar =(Toolbar) Findviewbyid (R.id.toolbar);9 Setsupportactionbar (toolbar);TenMdrawerlayout =(drawerlayout) Findviewbyid (r.id.drawer_layout); OneActionBar ActionBar =Getsupportactionbar (); A         if(ActionBar! =NULL){ -             //let Actionbar's Homeasup button show up . -Actionbar.setdisplayhomeasupenabled (true); the             //Change the icon of the Homeasup button - Actionbar.sethomeasupindicator (r.drawable.ic_action_name); -         } -  +     } -     ... +      Public Booleanonoptionsitemselected (MenuItem item) { A         Switch(Item.getitemid ()) { at              CaseAndroid. R.id.home: - Mdrawerlayout.opendrawer (gravitycompat.start); -                  Break; -     ... -             default: -                  Break; in         } -         return true; to     } +}

android--Sliding Menu

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.