The first type: slidingpanelayout
<android.support.v4.widget.SlidingPaneLayout Android:id= "@+id/sliding_pane_layout"xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Org.mobiletrain.a7_2slidingpanelayout." Mainactivity "> <!--slidingpanelayout layout is divided into two parts, the first part of the left side-pull menu, the second part of the main page-<LinearLayout Android:id= "@+id/left_layout"Android:layout_width= "200DP"Android:layout_height= "Match_parent"Android:background= "#f90101"android:gravity= "Center"android:orientation= "Vertical" > <ListView Android:id= "@+id/lv"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" ></ListView> </LinearLayout> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "#08f504"android:orientation= "Vertical" > <Android.support.v7.widget.Toolbar Android:id= "@+id/tool_bar"Android:layout_width= "Match_parent"Android:layout_height= "? Attr/actionbarsize"Android:background= "@color/colorprimary" ></android.support.v7.widget.Toolbar> <Button Android:id= "@+id/toggle"Android:layout_width= "Wrap_content"Android:onclick= "Toggle"Android:layout_height= "Wrap_content"/> <TextView Android:id= "@+id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Main Page"/> </LinearLayout></android.support.v4.widget.SlidingPaneLayout>
Public classMainactivityextendsappcompatactivity {PrivateList<string>list; PrivateSlidingpanelayout slidingpanelayout; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Toolbar Toolbar=(Toolbar) Findviewbyid (R.id.tool_bar); Setsupportactionbar (toolbar); ListView ListView=(ListView) Findviewbyid (r.id.lv); FinalTextView TextView =(TextView) Findviewbyid (r.id.tv); LinearLayout Leftlayout=(LinearLayout) Findviewbyid (r.id.left_layout); Viewgroup.layoutparams Layoutparams=Leftlayout.getlayoutparams (); //convert 100px to 100DPLayoutparams.width = (int) typedvalue.applydimension (TYPEDVALUE.COMPLEX_UNIT_DIP, 100, Getresources (). Getdisplaymetrics ()); Leftlayout.setlayoutparams (Layoutparams); Slidingpanelayout=(slidingpanelayout) Findviewbyid (r.id.sliding_pane_layout); InitData (); Arrayadapter<String> adapter =NewArrayadapter<string> ( This, Android. R.layout.simple_list_item_1, list); Listview.setadapter (adapter); Listview.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) {Textview.settext (List.get (position)); //Close the Side drop menuSlidingpanelayout.closepane (); } }); } //Click the Back button@Override Public voidonbackpressed () {//if the side-pull menu is open, close the side-drop menu when you click the Back button, or exit the app directly if(Slidingpanelayout.isopen ()) {Slidingpanelayout.closepane (); } Else { Super. onbackpressed (); } } Private voidInitData () {list=NewArraylist<>(); List.add (Favorites); List.add (Wallet); List.add (Album); List.add (File); } Public voidToggle (view view) {//when the button is clicked, if the side-pull menu is already open, turn it off or open the side-pull menu if(Slidingpanelayout.isopen ()) {Slidingpanelayout.closepane (); } Else{Slidingpanelayout.openpane (); } }}
The second type: drawerlayout
<android.support.v4.widget.DrawerLayout Android:id= "@+id/drawer_layout"xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Org.mobiletrain.a7_2drawerlayout." Mainactivity "> <!--Drawerlayout is divided into three parts, the main page, the left and right sides of the side-pull menu android:layout_gravity= "Left"The Android:layout_gravity property indicates that the page is the left side drop-down menu, note that you want to add a background to the layout of the side drop menu.= "Right"property indicates that the page is on the right side pull-down menu Note the location of the three parts: the first part of the main interface is divided into the left side pull menu The third part is the right side pull menu if the side Drop menu is written before the main page, the Click event in the Side drop menu will expire .-<relativelayout Android:id= "@+id/main_layout"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" > <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "Lefttoggle"Android:text= "Lefttoggle"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:onclick= "Righttoggle"Android:text= "@string/righttoggle"/> <TextView Android:id= "@+id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Main Page"/> </RelativeLayout> <LinearLayout Android:id= "@+id/left_layout"Android:layout_width= "200DP"Android:layout_height= "Match_parent"android:layout_gravity= "Left"Android:background= "#00fa08"android:orientation= "Vertical" > <ListView Android:id= "@+id/lv"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" ></ListView> </LinearLayout> <LinearLayout Android:id= "@+id/right_layout"Android:layout_width= "200DP"Android:layout_height= "Match_parent"android:layout_gravity= "Right"Android:background= "#fa0212"android:orientation= "Vertical" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Right"/> </LinearLayout></android.support.v4.widget.DrawerLayout>
Public classMainactivityextendsappcompatactivity {PrivateDrawerlayout drawerlayout; PrivateList<string>list; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Drawerlayout=(drawerlayout) Findviewbyid (r.id.drawer_layout); ListView ListView=(ListView) Findviewbyid (r.id.lv); FinalTextView TextView =(TextView) Findviewbyid (r.id.tv); InitData (); Arrayadapter<String> adapter =NewArrayadapter<string> ( This, Android. R.layout.simple_list_item_1, list); Listview.setadapter (adapter); Listview.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) {Textview.settext (List.get (position)); Drawerlayout.closedrawer (Gravity.left); } }); } @Override Public voidonbackpressed () {if(Drawerlayout.isdraweropen (gravity.left)) {drawerlayout.closedrawer (gravity.left); } Else if(Drawerlayout.isdraweropen (gravity.right)) {drawerlayout.closedrawer (gravity.right); } Else { Super. onbackpressed (); } } Private voidInitData () {list=NewArraylist<>(); List.add (Favorites); List.add (Wallet); List.add (File); List.add (Album); } Public voidlefttoggle (view view) {//determine if the left side pull menu is open if(Drawerlayout.isdraweropen (gravity.left)) {//Close the left side pull menuDrawerlayout.closedrawer (Gravity.left); } Else { //Open the left side drop-down menuDrawerlayout.opendrawer (Gravity.left); } } Public voidrighttoggle (view view) {//Open the right side pull-down menuDrawerlayout.opendrawer (gravity.right); }}
Side-Pull Menu