Sidebar is a common interface effect in Android applications (drawer effect). Using Drawerlayout to implement the right-hand column is relatively straightforward. And this control has its own sliding effect, very convenient.
Drawerlayout belongs to the contents of the Android-support-v4.jar package, the SDK new will not be updated, if the old version of the need to import the package.
Let's see the effect first.
Here the drawer effect is realized and in order to facilitate the user to be able to open this drawer everywhere, I define here in the Click menu can appear in the drawer.
Code description
1.activity Layout file
<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" > <!--note the naming of Drawrlayout, as a result of the use of--> <framelayout android:id= "@+id/content_ Frame "android:layout_width=" match_parent "android:layout_height=" match_parent "> <fragment android:id=" @+id /fragment_hello "Android:name=" com.patent.patentwarmsystem.CorrelationFragment "android:layout_width=" Wrap_ Content "android:layout_height=" wrap_content "/> </FrameLayout> <include layout=" @layout/activity_mai N_right "android:id=" @+id/right_drawer "android:layout_width=" 240DP "android:layout_height=" Match_parent "Android: Layout_gravity= "End" android:background= "#FFFFFF" android:gravity= "Center_horizontal" > </include> </an Droid.support.v4.widget.drawerlayout>
You can see that drawlayout can be used as a control, and then define the layout of the sidebar, which contains a layout that can be defined (arbitrarily). But be aware that this right layout property android:layout_gravity= "End". This is where the right is placed.
Placed on the left is: android:layout_gravity= "Start"
2.activity of code use
First of all pay attention to import this: import android.support.v4.widget.DrawerLayout;
public class Mainactivity extends Appcompatactivity {private drawerlayout drawerlayout;
Private Actionbardrawertoggle Toggle;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initdrawerlayout (); Drawerlayout.setscrimcolor (Color.gray); This can be set drawer pull out after the main interface color, I use the system here with the gray} private void Initdrawerlayout () {//Note: Initialization is drawerlayout the entire large layout, not the initialization drawer of that ID D
Rawerlayout = (drawerlayout) Super.findviewbyid (r.id.drawer_layout);
Drawerlayout.setscrimcolor (color.transparent); V4 Control Actionbar on the drawer switch, you can implement the dynamic effect of some switches toggle = new Actionbardrawertoggle (this, drawerlayout, R.drawable.star_change, R.string.drawer_open, r.string.drawer_close) {public void ondrawerclosed (View drawerview) {Super.ondrawerclo SED (drawerview);//drawer closed after} public void ondraweropened (View drawerview) {super.ondraweropened (Drawerview);/drawer Open
After}}; Drawerlayout.setdrawerlistener (TOGGLe); /** * Load Menu/@Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds items T
o The Action Bar if it is present.
Getmenuinflater (). Inflate (R.menu.menu, menu);
return true; //above to make it easy for users to call this method, just call this method binding ID to control the drawer pull out of the private void togglerightsliding () {//The method controls the display and hide of the right sidebar if ( Drawerlayout.isdraweropen (Gravitycompat.end)) {drawerlayout.closedrawer (gravitycompat.end);/close drawer}else{drawerLa Yout.opendrawer (gravitycompat.end)//Open Drawer}} @Override public boolean onoptionsitemselected (MenuItem item) {Swit
CH (item.getitemid ()) {case r.id.action_personal:togglerightsliding ();
Break
return super.onoptionsitemselected (item);
}
}
In addition to a number of ways to facilitate the reader's needs change:
Through code: Open the specified drawer
drawerlayout.opendrawer (gravity.left) According to the gravity direction;
Set drawer Shadow
Drawerlayout.setdrawershadow (R.drawable.ic_launcher, gravity.left);
Set the drawer in the vacant place color
drawerlayout.setscrimcolor (color.blue);
Well, the ability to implement a drawer is the code, you can see that the custom space is still very large.