Use of the Android slide menu Drawerlayout

Source: Internet
Author: User


Now the side-slip menu is used a lot, mostly through Slidingmenu. You can now also pass Drawerlayout

Create a drawer layout


Frament_content.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <textview        android:id= "@+id/textview" android:layout_width= "Match_        Parent "        android:layout_height=" wrap_content "        android:textsize=" 25SP "/></linearlayout>
Activity_main.xml

< 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 "> <!--the main        Content View-<framelayout android:id= "@+id/content_frame" android:layout_width= "Match_parent" android:layout_height= "Match_parent" > </FrameLayout> <!--the Navigation view and <LISTV        Iew android:id= "@+id/left_drawer" android:layout_width= "240DP" android:layout_height= "Match_parent" android:layout_gravity= "Start" android:background= "#ffffcc" android:choicemode= "Singlechoice" a Ndroid:divider= "@android: Color/transparent" android:dividerheight= "0DP" > </listview></android.suppo Rt.v4.widget.drawerlayout> 

Then create a new class to inherit the Fragment class

/** * Contentfragment.java * All rights reserved (C) 2015 * created by: Cuiran 2015-1-3 pm 3:25:44 */package Com.cayden.drawerlayoutdemo;import and Roid.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.View;import Android.view.viewgroup;import android.widget.textview;/** * TODO  * @author Cuiran * @version 1.0.0 */public class Con Tentfragment extends Fragment {private TextView TextView; @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup Container,bundle savedinstancestate) {View view = Inflater.inflate (R.layout.fragment_content, container, FALSE); TextView = (TextView) View.findviewbyid (R.id.textview); String text = Getarguments (). getString ("text"); Textview.settext (text); return view;}}

Complete Activity Code

Package Com.cayden.drawerlayoutdemo;import Java.util.arraylist;import Android.app.activity;import Android.app.fragment;import Android.app.fragmentmanager;import Android.content.intent;import Android.content.res.configuration;import Android.net.uri;import Android.os.bundle;import Android.support.v4.app.actionbardrawertoggle;import Android.support.v4.widget.drawerlayout;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.widget.AdapterView;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.ListView ;p ublic class Mainactivity extends Activity implements Onitemclicklistener {private Drawerlayout mdrawerlayout;private ListView mdrawerlist;private arraylist<string> menulists;private arrayadapter<string> adapter;private Actionbardrawertoggle mdrawertoggle;private String mtitle, @Overrideprotected void OnCreate (Bundle savedinstancestate ) {super.oncreate (savedinstancestate); setcontentView (r.layout.activity_main); mtitle = (String) getTitle (); mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawer_ layout); mdrawerlist = (ListView) Findviewbyid (r.id.left_drawer); menulists = new arraylist<string> (); for (int i = 0 ; I < 5; i++) menulists.add ("menu 0" + i); adapter = new Arrayadapter<string> (this,android. R.layout.simple_list_item_1, menulists); Mdrawerlist.setadapter (adapter); Mdrawerlist.setonitemclicklistener (this ); mdrawertoggle = new Actionbardrawertoggle (this, Mdrawerlayout,r.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {@Overridepublic void ondraweropened (View drawerview) {super.ondraweropened (Drawerview); Getactionbar (). Settitle ("Please select"); Invalidateoptionsmenu (); Call Onprepareoptionsmenu ()} @Overridepublic void ondrawerclosed (View drawerview) {super.ondrawerclosed (Drawerview ); Getactionbar (). Settitle (Mtitle); Invalidateoptionsmenu ();}; Mdrawerlayout.setdrawerlistener (Mdrawertoggle);//Turn on the function of app icon on Actionbar getactionbar (). SetdisplayhomeasupenabLED (true); Getactionbar (). Sethomebuttonenabled (true);} @Overridepublic boolean Onprepareoptionsmenu (Menu menu) {Boolean isdraweropen = Mdrawerlayout.isdraweropen ( mdrawerlist); Menu.finditem (R.id.action_websearch) setvisible (!isdraweropen); return Super.onprepareoptionsmenu ( menu);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//combines the icon on the Actionbar with the drawer if ( Mdrawertoggle.onoptionsitemselected (item)) {return true;} Switch (Item.getitemid ()) {case R.id.action_websearch:intent Intent = new Intent (); Intent.setaction (" Android.intent.action.VIEW "); Uri uri = uri.parse ("http://www.baidu.com"); Intent.setdata (URI); startactivity (intent); break;} return super.onoptionsitemselected (item);} @Overrideprotected void Onpostcreate (Bundle savedinstancestate) {super.onpostcreate (savedinstancestate);// Need to be ActiondrawertoGgle the status of Drawerlayout with//drawer icon in Actionbardrawertoggle set to Actionbar Home-button in Iconmdrawertoggle.syncstate ();} @Overridepublic void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig); Mdrawertoggle.onconfigurationchanged (newconfig);} @Overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int position,long arg3) {// Dynamically inserting a fragment into framelayout fragment contentfragment = new Contentfragment (); Bundle args = new bundle (); Args.putstring ("Text", Menulists.get (position)); contentfragment.setarguments (args); Fragmentmanager fm = Getfragmentmanager (); Fm.begintransaction (). replace (R.id.content_frame, contentfragment). Commit (); Mdrawerlayout.closedrawer (mdrawerlist);}}




Use of the Android slide menu Drawerlayout

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.