DrawerLayout是Android中的一个控件,效果是一个侧拉的效果。有两部分组成,一个侧拉的布局,一个是主页面的布局。
1.在布局文件中定义DrawerLayout控件
2.设置侧拉的方向, android:layout_gravity=”start”,表示从左向右
start换为end则是从右向左
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="wangxuewei.bwie.com.week3.MainActivity">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fr_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_shouye"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/rb_select"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="首页" />
<RadioButton
android:id="@+id/rb_faxian"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/rb_select"
android:button="@null"
android:gravity="center"
android:text="发现" />
<RadioButton
android:id="@+id/rb_xiazai"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/rb_select"
android:button="@null"
android:gravity="center"
android:text="下载" />
<RadioButton
android:id="@+id/rb_wode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/rb_select"
android:button="@null"
android:gravity="center"
android:text="我的" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="钱包" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="行程记录" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="好友邀请" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
效果展示,效果比较丑,里面的布局都是可以自己定义的,正常布局就可以了。
drawerLayout.closeDrawer(侧拉布局的id); 关闭菜单
drawerLayout.openDrawer(侧拉布局的id);打开菜单
监听事件
drawerLayout.setDrawerListener
更多详情,请参考:http://blog.youkuaiyun.com/liujiawei00/article/details/51427098