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

<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控件在布局文件中引入就可以了,不用写其他的用户交互代码就可以有侧滑效果。修改完activity_main.xml文件后运行程序就可出现文章开头的效果。

功能拓展


我们可以利用抽屉动画模拟QQ界面,效果如下:

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

主界面使用ImageView插入图片,图片放在app-res-drawable中,可直接从剪贴板粘贴添加图片,注意图片命名。接下来两个LinearLayout线性布局实现账号密码输入,每个LinearLayout包含TextView文本控件和EditText文本编辑控件,下方添加一个Button按钮提交登录信息。

左侧界面线性布局和表格布局结合。每一行是一个LinearLayout,每个LinearLayout包含一个TableLayout,每个TableLayout包含3个文本框,文本框的宽度由每个文本框的layout_weight按比例控制。

右侧界面插入一张图片,方法同上。使用ImageView,background属性会根据ImageView的大小进行伸缩。若把background改成src则会以原图大小显示

以下是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”>

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background=“#E6E6E6”

android:orientation=“vertical”>

<ImageView

android:id=“@+id/iv”

android:layout_width=“70dp”

android:layout_height=“70dp”

android:layout_centerHorizontal=“true”

android:layout_marginTop=“40dp”

android:background=“@drawable/head”/>

<LinearLayout

android:id=“@+id/ll_number”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_below=“@id/iv”

android:layout_centerVertical=“true”

android:layout_marginBottom=“5dp”

android:layout_marginLeft=“10dp”

android:layout_marginRight=“10dp”

android:layout_marginTop=“15dp”

android:background=“#ffffff”>

<TextView

android:id=“@+id/tv_number”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:padding=“10dp”

android:text=“账号:”

android:textColor=“#000”

android:textSize=“15sp”/>

<EditText

android:id=“@+id/et_number”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginLeft=“5dp”

android:background=“@null”

android:padding=“10dp”/>

<LinearLayout

android:id=“@+id/ll_password”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_below=“@id/ll_number”

android:layout_centerVertical=“true”

android:layout_marginLeft=“10dp”

android:layout_marginRight=“10dp”

android:background=“#ffffff”>

<TextView

android:id=“@+id/tv_password”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:padding=“10dp”

android:text=“密码:”

android:textColor=“#000”

android:textSize=“15sp”/>

<EditText

android:id=“@+id/et_password”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginLeft=“5dp”

android:layout_toRightOf=“@id/tv_password”

android:background=“@null”

android:inputType=“textPassword”

android:padding=“10dp”/>

<Button

android:id=“@+id/btn_login”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_below=“@id/ll_password”

android:layout_marginLeft=“10dp”

android:layout_marginRight=“10dp”

android:layout_marginTop=“50dp”

android:background=“#3C8DC4”

android:text=“登录”

android:textColor=“#ffffff”

android:textSize=“20sp”/>

<LinearLayout

android:layout_width=“200sp”

android:layout_height=“match_parent”

android:layout_gravity=“left”

android:background=“#aaaaaa”

android:orientation=“vertical”>

<TableRow

android:layout_width=“match_parent”

android:layout_height=“30dp”>

<TextView

android:layout_width=“0px”

android:layout_height=“wrap_content”

android:layout_weight=“4”

android:text=“了解会员特权” />

<TextView

android:layout_width=“0px”

android:layout_height=“wrap_content”

android:layout_weight=“3”

android:text=" " />

<TextView

android:layout_width=“0px”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:hint=“>” />

<TableRow

android:layout_width=“fill_parent”

android:layout_height=“30dp”>

<TextView

android:layout_width=“0px”

android:layout_height=“wrap_content”

android:layout_weight=“4”

android:text=“QQ钱包” />

<TextView

android:layout_width=“0px”

android:layout_height=“wrap_content”

android:layout_weight=“3”

android:text=" " />

<TextView

android:layout_width=“0px”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:hint=“>” />

<TableRow

android:layout_width=“fill_parent”

android:layout_height=“30dp”>

<TextView

android:layout_width=“0px”

android:layout_height=“wrap_content”

android:layout_weight=“4”

android:text=“个性装扮” />

<TextView

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

[外链图片转存中…(img-JWJ9xgqF-1715895906954)]

[外链图片转存中…(img-9V1bWVXP-1715895906956)]

[外链图片转存中…(img-4MCyhsQN-1715895906958)]

[外链图片转存中…(img-ZXLV3fog-1715895906959)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 25
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值