Drawerlayout Preview Drawerlayout The main function is to realize the function of the side-by-side menu effect, and can add some settings to achieve the high effect, then see the dynamic diagram:
Attention to the upper left corner of the icon, there is a lot of wood fun, haha ...
The next step is to introduce how to implement this feature 1. Add dependencies to the build.gradle of the project
Dependencies { ... // other code ' com.android.support:appcompat-v7:24.0.0 ' ' com.android.support:design:24.0.0 ' ... // other code }
2. Add toolbar to create the Toolbar.xml file
<?xml version="1.0"encoding="Utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:app="Http://schemas.android.com/apk/res-auto"Android:layout_width="wrap_content"Android:layout_height="wrap_content"> <Android.support.v7.widget.Toolbar Android:id="@+id/toolbar"android:cliptopadding="true"android:fitssystemwindows="true"Android:layout_width="match_parent"Android:layout_height="wrap_content"App:title="News"App:titletextcolor="#fff"> </android.support.v7.widget.Toolbar> </RelativeLayout>
3. Add Drawerlayout to Main.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:orientation="Vertical"> <!--Add Toolbar--<include layout="@layout/toolbar"/> <!--add Drawerlayout--> <android.support.v4.widget.DrawerLayout Android:id="@+id/drawerlayout"Android:layout_width="match_parent"Android:layout_height="match_parent"> <!--the first-in-one representation of the main content--<framelayout Android:id="@+id/main"Android:layout_width="match_parent"Android:layout_height="match_parent"> </FrameLayout> <!--left side menu (set Layout_gravity to leftmost)--<relativelayout Android:id="@+id/left"Android:layout_width="match_parent"Android:layout_height="match_parent"android:layout_gravity=" Left"> </RelativeLayout> <!--the right-hand menu (set to Layout_gravity)--<relativelayout Android:id="@+id/right"Android:layout_width="match_parent"Android:layout_height="match_parent"android:layout_gravity=" Right"> </RelativeLayout> </android.support.v4.widget.DrawerLayout> </LinearLayout>
Drawerlayout generally divided into three parts main content, left menu, right menu
The content of each section is set by itself, I use the fragment way to set the content, here is only for reference
// new fragment, details I'm not going to say anything. New Fragmentmain (); // add content, relatively simple Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.main, Fragmentmain). commit ();
So far, the function of the slide-by menu has been initially realized, so look at the effect
Drawerlayout initial effect. gif then, it is to add the effect to the side-slip button 1. To initialize the view before
Mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawerlayout); This . Findviewbyid (R.id.toolbar); Setsupportactionbar (toolbar);
2. Through actionbardrawertoggle to achieve the effect, the operation is very simple
New Actionbardrawertoggle (homeactivity. this, mdrawerlayout, toolbar, R. string . Open, R. string . close); Mtoggle.syncstate (); Mdrawerlayout.adddrawerlistener (mtoggle);
So it's over.
Finally, solve the problem that drawerlayout can't slide full screen.
Private voidSetdrawerleftedgesize (activity activity, drawerlayout drawerlayout,floatdisplaywidthpercentage) { if(Activity = =NULL|| Drawerlayout = =NULL)return; Try { //Locate Viewdraghelper and set Accessible to TrueField Leftdraggerfield =Drawerlayout.getclass (). Getdeclaredfield ("Mleftdragger");// RightLeftdraggerfield.setaccessible (true); Viewdraghelper Leftdragger= (viewdraghelper) Leftdraggerfield.Get(drawerlayout); //Locate Edgesizefield and set Accessible to TrueField Edgesizefield = Leftdragger.getclass (). Getdeclaredfield ("medgesize"); Edgesizefield.setaccessible (true); intEdgesize =Edgesizefield.getint (Leftdragger); //set a new edge sizePoint displaysize =NewPoint (); Activity.getwindowmanager (). Getdefaultdisplay (). GetSize (Displaysize); Edgesizefield.setint (Leftdragger, Math.max (Edgesize, (int) (Displaysize.x *( displaywidthpercentage))); } Catch(Nosuchfieldexception e) {}Catch(IllegalArgumentException e) {}Catch(Illegalaccessexception e) {}}
Call this method directly! The last parameter, 1, allows full-screen swipe. If you want the right menu to be full-screen, just change the corresponding "Mleftdragger" to "Mrightdragger".
Android tall on the Slide menu drawerlayout, solves the problem of not being able to slide full screen