DrawerLayout是SupportLibrary包中实现的侧滑菜单效果的控件。
分为主内容区域和侧边菜单区域
drawerLayout本身就支持:侧边菜单根据手势展开与隐藏,
开发者只需要实现:主内容区的内容和菜单的点击变化即可。
API:https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
1. DrawerLayout本身就是一个顶级容器,只需要按照规定的布局方式写布局就会有侧滑效果:
规定:
- DrawerLayout要包括两个子视图,分别为主内容区布局和侧滑菜单布局,并且主内容区布局要放到侧滑菜单布局的前面;
- 主内容布局需要设置宽高为match_parent,且不能设置layout_gravity;
- 侧滑菜单布局一般高度为match_parent,宽度为固定值;
- 侧滑菜单布局需要设置layout_gravity属性,start表示左边,end表示右边,不设置时会默认显示且不能滑动;
- 侧滑菜单布局(drawer)只能有一个,多于一个会报错;
- 主内容布局和侧滑菜单布局,使用何种样式的布局没有强制限制。
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/palyer_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".PlayerActivity" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="200dp" android:layout_marginLeft="200dp" android:text="这是主区域"/> </RelativeLayout> <LinearLayout android:orientation="vertical" android:layout_width="200dp" android:layout_gravity="start" android:layout_height="match_parent" android:background="#ff00B8D4"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:text="这是菜单栏"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="菜单一"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="菜单二"/> </LinearLayout> </android.support.v4.widget.DrawerLayout>