First on the code:
Layout file
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/ Apk/res/android "xmlns:tools="http://schemas.android.com/tools "android:id=" @+id/id_drawerlayout "android:layout_width=" Match_parent "android:layout_height ="Match_parent" > <!--main interface -- <framelayoutandroid:id="@+id/id_framelayout"android:layout_width= "Match_parent" android:layout_height="Match_parent" > <buttonandroid:id="@+id/btn_open_drawer"android:layout_width= "Wrap_content" android:layout_height="Wrap_content"android:text="Open drawer" /> </framelayout> <!--drawers -- <linearlayoutandroid:id= "@+id/id_drawer"android:layout_width=" 240DP "android:layout_height="match_parent "android:layout_gravity=" Start "android:background=" #E0EEE0 "android:orientation=" Vertical " > <ListViewandroid:id="@+id/id_lv"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:divider=" #CD853F " android:dividerheight="2DP" > </ListView> </linearlayout> </android.support.v4.widget.DrawerLayout>
Initialize Drawerlayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Set Drawerlayout.drawerlistener as a listener class with four callback functions
Mdrawerlayout.setdrawerlistener (NewDrawerlistener () {/** * The state value is called when the drawer sliding state is changed State_idle (Idle--0), state_dragging (dragged--1), state_settling (fixed--2) in one. * When the drawer is open, click on the drawer, the state of drawer will become state_dragging, and then become State_idle * / @Override Public void ondrawerstatechanged(intarg0) {LOG.I ("Info","The state of drawer:"+ arg0); }/** * This method is called when the drawer is sliding * ARG1 indicates the amplitude of the slide (0-1) */ @Override Public void Ondrawerslide(View arg0,floatARG1) {LOG.I ("Info", Arg1 +""); }/** * is called when a drawer is fully opened */ @Override Public void ondraweropened(View arg0) {LOG.I ("Info","The drawer is completely open!" "); }/** * Call this method when a drawer is completely closed */ @Override Public void ondrawerclosed(View arg0) {LOG.I ("Info","The drawer is completely closed!" "); } });
Set the button's listener, click to open the drawer
/*** 打开抽屉*/findViewById(R.id.btn_open_drawer).setOnClickListener(new OnClickListener() { @Override publicvoidonClick(View v) { // 按钮按下,将抽屉打开 mDrawerLayout.openDrawer(Gravity.LEFT); } });
Drawer menu (Drawerlayout)