熟悉又陌生的ConstraintLayout布局
yuyutoo 2024-10-19 11:08 8 浏览 0 评论
中文名:约束布局
出生日期:2016年
- 为什么熟悉?
ConstraintLayout是近几年I/O大会力推功能之一,从Android Studio 2.3开始新建布局默认就是它,而且基本上每一次Studio升级日志里都能见到它的身影,所谓没吃过猪肉也肯定见过猪跑。 - 为什么陌生?
约束布局可以说是有史以来功能最复杂的布局了,很多人可能没有沉下来心来好好了解一下这个布局,新建布局后第一步就是刷刷地删掉默认根布局,默默写上我们熟悉的LinearLayout和RelativeLayout;或者没有好的场景应用到实际项目中,我们的新版本完全搬到RN上来了,根本不可能应用到它。
1. 介绍
ConstraintLayout可以说是LinearLayout、FrameLayout、RelativeLayout等等传统布局的集大成者,天生就是消灭嵌套布局。相比RelativeLayout的组件间的关系,ConstraintLayout将这层关系更升华了,通过更加灵活的约束,让布局更加扁平化,往往使用一层布局就能实现很多复杂需求。
底层原理上,ConstraintLayout使用了和iOS的Auto Layout相同的算法火鸡算法 Cassowary,让其具备复杂功能的同时比传统布局更加高效。
2. 使用
ConstraintLayout配合Android Studio食用更佳。我们知道,Studio一直针对ConstraintLayout作改进,大部分是优化ConstraintLayout的可视化,尤其是蓝图,也就是下面图中右边的蓝色部分:
以前的线性布局、关系布局啥的都比较不适合用设计面板来拖拽,而约束布局恰恰相反,用蓝图能更清楚的理清组件间的关系,并且通过拖拽和指定属性的方式来建立复杂关系代码反而不好写。
当然从实践来看,最好还是将代码和可视化结合起来,先用代码写个View,再用拖拽的方式建立大致的约束关系,再回到代码进行微调。拖拽的方式生成的代码比较乱,界面元素较多时很容易拖拽出错,而且很容易拖出很多零散的属性值出来,需要到代码上进行比较的调整和删减。
如上图,选中某个组件就可以查看该组件的约束关系,比如这个ImageView,波浪线就是表示图片的left、top、bottom关联到根布局且边距为8dp,bottom关联到TextView上方并且间距50dp(这里TextView的约束关系是根布局居中),layout_width和layout_height都是0,表示大小充满约束空间MATCH_CONSTRAINT(类似一般布局的MATCH_PARENT)。因此这张图片最终显示将显示在屏幕的上半部分。
常用属性
相对位置
和确定组件位置相关常用的就是下面13个属性(也就是蓝图中的波浪线),见名知意,和RelativeLayout的功能基本重叠,这里也不细介绍了。可以自己妥妥拽拽试试,简单场景足够。
- layout_constraintLeft_toLeftOf
- layout_constraintLeft_toRightOf
- layout_constraintRight_toLeftOf
- layout_constraintRight_toRightOf
- layout_constraintTop_toTopOf
- layout_constraintTop_toBottomOf
- layout_constraintBottom_toTopOf
- layout_constraintBottom_toBottomOf
- layout_constraintBaseline_toBaselineOf
- layout_constraintStart_toEndOf
- layout_constraintStart_toStartOf
- layout_constraintEnd_toStartOf
- layout_constraintEnd_toEndOf
上图的ImageView对应的代码就是
<ImageView
tools:src="@mipmap/ic_launcher"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="50dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
这个相对位置有个比较有意思的应用,比如说有这么个需求,如下图,文本组件要放置在图片组件的中间。
可以这么实现,将TextView四边分别约束到ImageView的四边上即可,那么不管ImageView显示在哪,这个TextView永远跟随在其中间。代码如下:
<ImageView
android:id="@+id/img"
android:src="@mipmap/ic_launcher"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="18sp"
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="@id/img"
app:layout_constraintLeft_toLeftOf="@id/img"
app:layout_constraintRight_toRightOf="@id/img"
app:layout_constraintTop_toTopOf="@id/img"
android:id="@+id/textView"/>
如果不使用ConstraintLayout,这个需求可能就要增加一个RelativeLayout套在ImageView和TextView来实现了。
当上下或者左边都是约束后,默认这个组件就是横向或者纵向居中了。有时候我们要的不是居中,而且以一定比例偏离,比如水平居中,垂直方向偏下,那么可以通过下面两个属性设置限制位置比例,取值0~1.0:
- layout_constraintHorizontal_bias 水平方向的比例
- layout_constraintVertical_bias 垂直方向比例
如下是Vertical=0.8的效果:
圆形定位
通过圆形定位可以以一定的角度和距离确定位置关系,这是RelativeLayout所没有的。
相关属性:
- layout_constraintCircle:参照控件的id
- layout_constraintCircleRadius:两个控件中心连线的距离
- layout_constraintCircleAngle:当前View的中心与目标View的中心的连线与Y轴方向的夹角(取值:0~360)
宽高尺寸
在ConstraintLayout中推荐的LayoutParams值有
- 具体dp
- WRAP_CONTENT
- 0dp,表示MATCH_CONSTRAINT,这里没有MATCH_PARENT啥事
0dp是一个比较有意思的值,可以类比LinearLayout的0dp+weight组合,但是功能更加丰富。
在这里0dp默认是weight=1的效果,可以通过下面的6个属性进一步控制大小:
- layout_constraintWidth_min
- layout_constraintHeight_min
- layout_constraintWidth_max
- layout_constraintHeight_max
- layout_constraintWidth_percent
- layout_constraintHeight_percent
这6个属性都让人忍不住点赞,宽高区间以及占被约束对象的百分比一应俱全。
宽高比
当宽或高其中一个设置为0dp时,可以通过layout_constraintDimensionRatio来设置宽高比。
这是一个很赞的功能,很多实际场景很需要这个,比如在一些流式布局场景中,往往需要以固定的比例显示item;或者页面的横幅等等,这个都要求不受全面屏等形形色色的屏幕比例影响。
举例,一个横幅(ImageView)使用以下属性:
<ImageView
android:id="@+id/img"
android:src="@mipmap/ic_launcher"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
/>
那么这个横幅会页面上方保持16:9的比例显示,并且横向填充满屏幕。
当宽高都为0dp时,layout_constraintDimensionRatio还可以加上W或者H。
比如:
W,1:2表示宽=2,高=1,即H:W = 1:2;
H,1:2表示高=2,宽=1,即W:H = 1:2。
不使用ConstraintLayout的情况下要实现类似功能,恐怕就不得不在Java代码中重写onMeasure等方式来实现了。
边距
之所以要说边距,是因为在ConstraintLayout中,layout_margin(left|top|right|bottom)属性的含义有所变化,它仅在该View该方向上约束关系的情况下才有效,当它失去了约束,其边距便不会生效。
此外还有一组GONE Margins,仅当被约束的目标对象的可见性为View.GONE才生效,
- layout_goneMarginStart
- layout_goneMarginEnd
- layout_goneMarginLeft
- layout_goneMarginTop
- layout_goneMarginRight
- layout_goneMarginBottom
链
链其实很好理解,类似于LinearLayout,将一组View左右互相约束,就组成一组横向排列(纵向同理)
如果仅仅是线性排列,那就没什么特别的了。
举个例子,下面是三个TextView,和上图一样水平方向左右互联,垂直方向靠近容器底部。
<TextView
android:id="@+id/t1"
android:text="text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/t2"
/>
<TextView
android:id="@+id/t2"
android:text="text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/t1"
app:layout_constraintEnd_toStartOf="@id/t3"
/>
<TextView
android:id="@+id/t3"
android:text="text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/t2"
app:layout_constraintEnd_toEndOf="parent"
/>
看看实际结果(底部):
可以看出三个TextViewmore就是平分的,也就是LinearLayout中weight相同的情况。此外我们可以对链中第一个View也就是链头设置layout_constraintHorizontal_chainStyle属性改变调整居中的类型,值有三种:
- CHAIN_SPREAD —— 展开元素 (默认);
- CHAIN_SPREAD_INSIDE —— 展开元素,但链的两端贴近parent;
- CHAIN_PACKED —— 链的元素将被打包在一起
spread就是默认样式,三者分别在四等分线上(一条线段三刀切成均分四段),兄弟关系相敬如宾;
spread_inside效果如图,三者可以理解为包含首尾的二等分点,兄弟关系最疏远;
packed效果如果,三者紧贴一起,基情满满。
怀念LinearLayout的weight?也有的,通过给每个控件宽度设置为0dp,就可以通过layout_constraintHorizontal_weight属性控制权重了。
辅助工具
所谓辅助工具,就是指一些只提供功能但实际看不见(类似Space)的约束目标,比如中轴线之类的。
Barrier屏障
假设有3个控件ABC,C在AB的右边,但是AB的宽是不固定的,这个时候C无论约束在A的右边或者B的右边都不对。
当出现这种情况可以用Barrier来解决。Barrier可以在多个控件的一侧建立一个屏障,如下所示:
这个时候C只要约束在Barrier的右边就可以了,代码如下:
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/TextView1" />
<android.support.constraint.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="TextView1,TextView2" />
<TextView
android:id="@+id/TextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/barrier" />
Group分组
Group可以把多个控件归为一组,方便隐藏或显示一组控件,举个例子:
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/TextView1" />
<TextView
android:id="@+id/TextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/TextView2" />
现在有3个并排的TextView,用Group把TextView1和TextView3归为一组,再设置这组控件的可见性,如下所示:
<android.support.constraint.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:constraint_referenced_ids="TextView1,TextView3" />
效果如下:
PlaceHolder占位符
在Placeholder中可使用setContent()设置另一个控件的id,使这个控件移动到占位符的位置。举个例子:
<android.support.constraint.Placeholder
android:id="@+id/placeholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:content="@+id/textview"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#cccccc"
android:padding="16dp"
android:text="TextView"
android:textColor="#000000"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
新建一个Placeholder约束在屏幕的左上角,新建一个TextView约束在屏幕的右上角,在Placeholder中设置 app:content=”@+id/textview”,这时TextView会跑到屏幕的左上角。效果如下:
我理解的占位符的使用场景应该是:
- 整理框架结构,布局隔离
因为ConstraintLayout很扁平,可能xml文件杂糅了很多组件,尤其是可视化拖拽出来的控件,没有固定的顺序,很难拎清页面整个的主体结构。
那么,我们可以单独写一个xml文件,里面就只有页面整体的结构,比如横幅、列表、tab栏等等,都用PlaceHolder占坑,而具体的模块控件则在各自的xml上设计,二者通过contentId关联。通过include把placeHolder文件merge进来,组成一个完整页面。 - 代码动态控制控件位置
如下动画,在顶部横幅下方放置了一个PlaceHolder,然后根据业务逻辑在tab页进入不同模式后动态调用PlaceHolder#setContentId方法改变实际的View。(图中通过TransitionManager增加了过度动画)
Guideline 辅助线
辅助线就像设计图里面的网格线,辅助组件定位,最终不可见,常用的辅助线场景比如中轴线,组件可以与中轴线建立约束关系。
辅助线分为横向和纵向两种,通过orientation指定,有horizontal和vertical两种
然后通过下面3个属性之一来确定位置:
- layout_constraintGuide_percent 按比例确定位置,取值0~1.0
- layout_constraintGuide_begin 距离左边(或上边)的距离,dp值
- layout_constraintGuide_end 距离右边(或下边)的距离,dp值
如下代码就是分别定义垂直和水平方向的中轴线,percent=0.5:
<android.support.constraint.Guideline
android:id="@+id/guideline1"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
有了这些辅助线,我们就可以做一些更有趣的布局了。
比如下图红框中的两个按钮,两个一起水平居中。
看起来非常简单的需求,但是以前要实现这个布局可不简单,我们需要把这两个按钮放到一个Horizontal的LinearLayout里,再让这个LinearLayout在RelativeLayout的底部居中。
现在用中轴线来实现,代码如下:
<Button
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
app:layout_constraintEnd_toStartOf="@+id/guideline1"
android:layout_marginEnd="10dp"
android:layout_marginBottom="50dp"
app:layout_constraintBottom_toBottomOf="parent"
/>
<Button
android:text="注册"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
app:layout_constraintStart_toStartOf="@+id/guideline1"
android:layout_marginStart="10dp"
android:layout_marginBottom="50dp"
app:layout_constraintBottom_toBottomOf="parent"
tools:text="注册"/>
guideline1就是上面提到的垂直中轴线,我们只要和中轴线建立约束即可。
相关推荐
- 高一高二第一次月考认真作答(高二第一次月考的重要性)
-
正在进行高一、高二第一次月考,同学们正在认真完成化学试卷,研究考纲,探究考点,夯实基础,迎战高考!
- 山清水秀,盛世今朝(山清水秀出处)
-
万千星河,神州妖娆!山清水秀,盛世今朝!龙腾虎跃,锦绣前程!千里婵娟,祝福永远!
- 我校二模成绩已新鲜出炉(二模考试成绩)
-
充电加油备战高考,积极努力再拼一搏...
- Argon Design向瑞萨电子有限公司提供Argon Streams VP9许可证
-
英国剑桥--(美国商业资讯)--领先的先进视频验证解决方案提供商ArgonDesignLtd已与日本半导体公司瑞萨电子有限公司(RenesasElectronicsCorporation)签署...
- 高考倒计时75天(高考倒计时75天励志语)
-
今天是2022年3月24日星期四,距离2022年高考还有75天时间对于十八岁的高三学子来说,有些事情的确会影响你们的一生,但是没有一件事能决定你们的一生!努力的意义,就是:以后的日子里,放眼望去,全...
- 期中考试正在进行(期中考试在即)
-
转眼即瞬,期中考试已到,紧张忙碌的两个月学习,检验的时刻到了。让我们拿出信心和勇气,来挑战自我。面对考验,我们该做的就是沉着,冷静。让知识来一次次洗礼我们的灵魂,让失败和成功迎接一次次的成长。你们可以...
- 不要浪费了你NAS上的HDMI接口!详解华硕NAS上HDMI接口的妙用
-
不要浪费了你NAS上的HDMI接口!详解华硕NAS上HDMI接口的妙用之前我在本站分享我使用的华硕(ASUS)AS6704T...
- Java通过Kafka Streams库来实现数据流处理
-
#暑期创作大赛#...
- From abandoned mines to limpid streams waters: how banks profit from EOD
-
ByZENGYanglinInthecurrentpursuitofthe“dualcarbon”target(carbonpeakingandcarbonneutra...
- SPSS与Streams的集成实现实时预测
-
SPSSModeler是一个数据挖掘工作台,提供了一个可了解数据并生成预测模型的最先进的环境。Streams提供了一个可伸缩的高性能环境,对不断变化的数据进行实时分析,这些数据中包括传统结构的数据...
- Kafka Streams, 我还会再使用它吗?
-
DeeptiMittal4分钟阅读...
- 大数据Hadoop之——Kafka Streams原理介绍与简单应用示例
-
一、KafkaStreams概述官网文档:https://kafka.apache.org/32/documentation/streams/...
- Android上的TCP今天开始向用户推出,并将在下个月向所有用户提供
-
据extends网3月15日报道,Firefox今天宣布,其保护用户免受跟踪器攻击的全面cookie保护(TCP)功能现已在Android上可用。该功能默认启动模式,这样,跟踪器将无法收集有关用户的浏...
- Linux curl命令(linux curl命令安装)
-
Linuxcurl命令是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。作为一款强力工具,curl支持包括HTTP、H...
- go语言http服务入门详解(go语言http服务器)
-
当你在浏览器中输入URL时,实际上是在发送一个对Web页面的请求。该请求被发送到服务器。服务器的工作是获取适当的页面并将其作为响应发送回浏览器。在Web的早期,服务器通常读取服务器硬盘上HTML文件的...
你 发表评论:
欢迎- 一周热门
-
-
前端面试:iframe 的优缺点? iframe有那些缺点
-
带斜线的表头制作好了,如何填充内容?这几种方法你更喜欢哪个?
-
漫学笔记之PHP.ini常用的配置信息
-
推荐7个模板代码和其他游戏源码下载的网址
-
其实模版网站在开发工作中很重要,推荐几个参考站给大家
-
[干货] JAVA - JVM - 2 内存两分 [干货]+java+-+jvm+-+2+内存两分吗
-
正在学习使用python搭建自动化测试框架?这个系统包你可能会用到
-
织梦(Dedecms)建站教程 织梦建站详细步骤
-
【开源分享】2024PHP在线客服系统源码(搭建教程+终身使用)
-
2024PHP在线客服系统源码+完全开源 带详细搭建教程
-
- 最近发表
-
- 高一高二第一次月考认真作答(高二第一次月考的重要性)
- 山清水秀,盛世今朝(山清水秀出处)
- 我校二模成绩已新鲜出炉(二模考试成绩)
- Argon Design向瑞萨电子有限公司提供Argon Streams VP9许可证
- 高考倒计时75天(高考倒计时75天励志语)
- 期中考试正在进行(期中考试在即)
- 不要浪费了你NAS上的HDMI接口!详解华硕NAS上HDMI接口的妙用
- Java通过Kafka Streams库来实现数据流处理
- From abandoned mines to limpid streams waters: how banks profit from EOD
- SPSS与Streams的集成实现实时预测
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)