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

最后

我的面试经验分享可能不会去罗列太多的具体题目,因为我依然认为面试经验中最宝贵的不是那一个个具体的题目或者具体的答案,而是结束面试时,那一刻你的感受以及多天之后你的回味~

很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习,对此我整理了一些资料,需要的可以免费分享给大家

在这里小编分享一份自己收录整理上述技术体系图相关的几十套腾讯、头条、阿里、美团等公司的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

【算法合集】

【延伸Android必备知识点】

【Android部分高级架构视频学习资源】

**Android精讲视频领取学习后更加是如虎添翼!**进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水,赶快领取吧!

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
…(img-piBnBgGw-1715255083358)]

【算法合集】

[外链图片转存中…(img-ArVxMyrW-1715255083358)]

【延伸Android必备知识点】

[外链图片转存中…(img-hyPFqWqn-1715255083359)]

【Android部分高级架构视频学习资源】

**Android精讲视频领取学习后更加是如虎添翼!**进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水,赶快领取吧!

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值