Android comes with drawerlayout sliding menu

Source: Internet
Author: User

Inspired by Slidingmenu Android adds Drawerlayout to implement the swipe menu

Drawerlayout is inherited from the ViewGroup, where you can place the child view. Drawerlayout the main content area and menu area

Use the following method
1. Same as the normal linearlayout

<android. Support. V4. Widgets. DrawerlayoutXmlns:android="Http://schemas.android.com/apk/res/android"Android:id="@+id/drawer_layout"Android:layout_width="Match_parent"android:layout_height="Match_parent"> <framelayout android:id="@+id/content_frame"Android:layout_width="Match_parent"android:layout_height="Match_parent"/> <listview android:id="@+id/left_drawer"Android:layout_width="240DP"android:layout_height="Match_parent"android:layout_gravity="Start"Android:choicemode="Singlechoice"android:layout_gravity="left"Android:divider="@android: Color/transparent"android:dividerheight="0DP"Android:background="#111"/></android. Support. V4. Widgets. Drawerlayout>

Fragment is the main content, not necessarily with fragment, any view can be, the ListView is a menu, the same as any view can be.
Note: (1) The main content layout should be placed in front of the menu layout to help Drawerlayout to identify what the menu layout is and what is the content layout
(2) android:choicemode= "Singlechoice" is a statement which is the menu layout
Android:layout_gravity= "Left" is a side-by-side slide, this value can be changed

2 the display and the hidden state of the menu have a corresponding listener interface call, you can add the logic you want to implement

mdrawertoggle = new                  Actionbardrawertoggle (this , Mdrawerlayout, R.drawable.ic_drawer, R.string . Drawer_open, R.string . Drawer_close) {public  void ondrawerclosed  (View view) {} public  void  ondraweropened  (View drawerview) {}}; Mdrawerlayout.setdrawerlistener (mdrawertoggle);  

3. Automatic control of menu opening and closing in code
The Drawerlayout.closedrawer method is used to hide the side menu, and the Drawerlayout.opendrawer method is used to expand the side menu
4, sometimes we just want to click on a button to display the menu and do not want to let a swipe to appear menu, then this time will be blocked off the sliding function
Mdrawer_layout.setdrawerlockmode (drawerlayout.lock_mode_locked_closed); Turn off gesture swipe
Mdrawer_layout.setdrawerlockmode (drawerlayout.lock_mode_unlocked); Open gesture Swipe

Android comes with drawerlayout 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.