如下代碼 DrawerLayout mdrawerLayout; Button btn; ---------------------------------------------------------------------------------------------------------
如下代碼
DrawerLayout mdrawerLayout;
Button btn;
-----------------------------------------------------------------------------------------------------------------------以上為聲明組件,為簡便其餘過程省略
btn.setOnClickListener(new OnClickListener(){
mdrawerLayout.openDrawer(Gravity.LEFT);//這裡設置的方嚮應該跟下麵xml文件裡面的gravity方向相同,不然會報錯,start和LEFT都為從左邊出現
mdrawerLayout.closDrawers();//沒有參數,關閉所有的出現的抽屜
});
-----------------------------------------------------下麵是drawerLayout.xml----------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dl_left"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主界面剩餘的佈局將在以下進行-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"/>
</LinearLayout>
<!--抽屜拉開後的佈局,即ListView所在的佈局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="start">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_left_menu"
android:divider="@null">
</ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>