抽屉,之前都是用slidingmenu这个第三方,今天看了利用DrawerLayout实现抽屉,特整理出来,方便日后查阅
首先,DrawerLayout是v4包下的一个控件,他是一个类似Linearlayout一样的一个布局控件,只不过他本身带有可以滑动的功能
首先看布局
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#a7d6b194" android:text="btn1" /> <TextView android:id="@+id/lv" android:layout_width="200dp" android:background="#00ff00" android:layout_height="match_parent" android:layout_gravity="start" android:text="pjkpookplo"></TextView> </android.support.v4.widget.DrawerLayout>
DrawerLayout必须要作为一个跟标签,他可以存在子布局,并且他的字布局,从上往下依次是主布局和抽屉的布局
如上面 第一个Button即为没有抽屉时候的主界面的布局,第二个TextView即为抽屉里面的布局
因为手机需要检测是从哪里出现抽屉,所以DrawerLayout的第二个布局,即抽屉的布局,应该加上属性layout_gravity,他的值有三个left左菜单、right右菜单、start
现在只需要去activity里面setcontview加载一下布局,简单抽屉效果就已经出来了