熟悉又陌生的ConstraintLayout布局
yuyutoo 2024-10-19 11:08 4 浏览 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就是上面提到的垂直中轴线,我们只要和中轴线建立约束即可。
相关推荐
- 史上最全的浏览器兼容性问题和解决方案
-
微信ID:WEB_wysj(点击关注)◎◎◎◎◎◎◎◎◎一┳═┻︻▄(页底留言开放,欢迎来吐槽)●●●...
-
- 平面设计基础知识_平面设计基础知识实验收获与总结
-
CSS构造颜色,背景与图像1.使用span更好的控制文本中局部区域的文本:文本;2.使用display属性提供区块转变:display:inline(是内联的...
-
2025-02-21 16:01 yuyutoo
- 写作排版简单三步就行-工具篇_作文排版模板
-
和我们工作中日常word排版内部交流不同,这篇教程介绍的写作排版主要是用于“微信公众号、头条号”网络展示。写作展现的是我的思考,排版是让写作在网格上更好地展现。在写作上花费时间是有累积复利优势的,在排...
- 写一个2048的游戏_2048小游戏功能实现
-
1.创建HTML文件1.打开一个文本编辑器,例如Notepad++、SublimeText、VisualStudioCode等。2.将以下HTML代码复制并粘贴到文本编辑器中:html...
- 今天你穿“短袖”了吗?青岛最高23℃!接下来几天气温更刺激……
-
最近的天气暖和得让很多小伙伴们喊“热”!!! 昨天的气温到底升得有多高呢?你家有没有榜上有名?...
- CSS不规则卡片,纯CSS制作优惠券样式,CSS实现锯齿样式
-
之前也有写过CSS优惠券样式《CSS3径向渐变实现优惠券波浪造型》,这次再来温习一遍,并且将更为详细的讲解,从布局到具体样式说明,最后定义CSS变量,自定义主题颜色。布局...
- 你的自我界限够强大吗?_你的自我界限够强大吗英文
-
我的结果:A、该设立新的界限...
- 行内元素与块级元素,以及区别_行内元素和块级元素有什么区别?
-
行内元素与块级元素首先,CSS规范规定,每个元素都有display属性,确定该元素的类型,每个元素都有默认的display值,分别为块级(block)、行内(inline)。块级元素:(以下列举比较常...
-
- 让“成都速度”跑得潇潇洒洒,地上地下共享轨交繁华
-
去年的两会期间,习近平总书记在参加人大会议四川代表团审议时,对治蜀兴川提出了明确要求,指明了前行方向,并带来了“祝四川人民的生活越来越安逸”的美好祝福。又是一年...
-
2025-02-21 16:00 yuyutoo
- 今年国家综合性消防救援队伍计划招录消防员15000名
-
记者24日从应急管理部获悉,国家综合性消防救援队伍2023年消防员招录工作已正式启动。今年共计划招录消防员15000名,其中高校应届毕业生5000名、退役士兵5000名、社会青年5000名。本次招录的...
- 一起盘点最新 Chrome v133 的5大主流特性 ?
-
1.CSS的高级attr()方法CSSattr()函数是CSSLevel5中用于检索DOM元素的属性值并将其用于CSS属性值,类似于var()函数替换自定义属性值的方式。...
- 竞走团体世锦赛5月太仓举行 世界冠军杨家玉担任形象大使
-
style="text-align:center;"data-mce-style="text-align:...
- 学物理能做什么?_学物理能做什么 卢昌海
-
作者:曹则贤中国科学院物理研究所原标题:《物理学:ASourceofPowerforMan》在2006年中央电视台《对话》栏目的某期节目中,主持人问过我一个的问题:“学物理的人,如果日后不...
-
- 你不知道的关于这只眯眼兔的6个小秘密
-
在你们忙着给熊本君做表情包的时候,要知道,最先在网络上引起轰动的可是这只脸上只有两条缝的兔子——兔斯基。今年,它更是迎来了自己的10岁生日。①关于德艺双馨“老艺...
-
2025-02-21 16:00 yuyutoo
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 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)