大家可以把DrawerLayout 当成LinearLayout 等使用,主要注意两点:
1.添加布局 android.support.v4.widget.DrawerLayout 。
2.在DrawerLayout 下,设置底层布局和侧滑栏布局,同时设置侧滑栏效果android:layout_gravity,start 和end (left|right)即从左往右还是 从右往左出现。
<?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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!-- 这个布局为主页布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
</LinearLayout>
<!-- 这个布局为侧滑栏布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:fitsSystemWindows="true"
android:layout_gravity="start"></LinearLayout>
</android.support.v4.widget.DrawerLayout>
通过以上xml文件,就可以实现DrawerLayout效果,再根据业务功能实现侧滑栏布局等。
PS:当侧滑栏没数据时,可能会出现卡顿,有数据以后就没问题了!!