安卓使用DrawerLayout实现左右划效果(抽屉动画)以及简单布局
开发环境为Android 3.3.1 模拟设备:3.7 WVGA (Nexus One) API 28
在android 5.0 中新增了 DrawerLayout控件,此控件可以实现抽屉动画,展现侧滑效果。侧滑包括了左侧滑和右侧滑。在qq,酷狗音乐等都可以看到抽屉动画的应用
创建程序
首先我们可以创建一个名为DrawerLayout的程序。在界面布局文件activity_main.xml中引入DrawerLayout控件。DrawLayout控件需要使用全路径名称
<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>
<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>
<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"/>
</LinearLayout>
&