<android.support.v4.widget.DrawerLayout
以下是activity_main.xml完整代码:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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”
tools:context=“.MainActivity”>
<android.support.v4.widget.DrawerLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“#fff”
android:orientation=“vertical”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“match_parent”
android:text=“主界面”
android:layout_gravity=“center”
android:textSize=“20sp”/>
<LinearLayout
android:layout_width=“200sp”
android:layout_height=“match_parent”
android:layout_gravity=“left”
android:background=“#aaaaaa”
android:orientation=“vertical”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“match_parent”
android:text=“左侧滑界面”
android:layout_gravity=“center”
android:textSize=“20sp”/>
<LinearLayout
android:layout_width=“200sp”
android:layout_height=“match_parent”
android:layout_gravity=“right”
android:background=“#bbbbbb”
android:orientation=“vertical”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“match_parent”
android:text=“右侧滑界面”
android:layout_gravity=“center”
android:textSize=“20sp”/>
</android.support.v4.widget.DrawerLayout>
</android.support.constraint.ConstraintLayout>
从以上代码可以发现,在DrawerLayout标签中创建3个LinearLayout线性布局,分别为主界面布局,左侧侧滑界面,右侧侧滑界面。其中比较重要的是后两个LinearLayout布局的第一行的layout_width属性,可以用来设置界面划出的宽度。layout_gravity属性可以设置该布局的方位,left为左侧滑界面,right为右侧滑界面
DrawerLayout控件在布局文件中引入就可以了,不用写其他的用户交互代码就可以有侧滑效果。修改完ac