安卓使用DrawerLayout实现左右划效果(抽屉动画)以及简单布局

本文介绍了如何在Android环境中使用DrawerLayout创建左右划动的抽屉效果,详细讲解了如何配置布局文件activity_main.xml,包括主界面、左侧和右侧滑动界面的设置,并展示了如何通过设置layout_width和layout_gravity属性实现不同方向的滑动。此外,还探讨了如何扩展功能,如模拟QQ界面,添加账号登录和自定义左侧、右侧界面。
摘要由CSDN通过智能技术生成

安卓使用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>

&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值