Android开发之路UI篇:ConstraintLayout

个人公众号:

定期更新。一起交流学习吧

1、简介:

ConstraintLayout是由AndroidStudio2.2推出一套新的布局。它是为了解决布局嵌套的问题,并且

增加可视化编辑的xml文件。

2、基本使用:

 1:相对定位:

layout_constraintLeft_toLeftOf    将控件 A左边的约束 置于控件 B的左边的方向上。AB左对齐

layout_constraintLeft_toRightOf  将控件 A左边的约束 置于控件 B的右边的方向上。A的左边与B的右边对齐

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

 

注意:ConstraintLayout 必须指定左右约束和上下约束。

 

 

2:角度定位

app:layout_constraintCircle="@+id/TextView1"

app:layout_constraintCircleAngle="120"(角度)

app:layout_constraintCircleRadius="150dp"(距离)

指的是TextView2的中心在TextView1的中心的120度,半径为150dp,效果如下:

 

 

注意:角度定位不是很常用,了解

 

3: 常用margin

ConstraintLayout的边距常用属性如下:

android:layout_marginStart

android:layout_marginEnd

android:layout_marginLeft

android:layout_marginTop

android:layout_marginRight

android:layout_marginBottom

 

goneMargin主要用于约束的控件可见性被设置为gone的时候使用的margin值,属性如下:

layout_goneMarginStart 

layout_goneMarginEnd

layout_goneMarginLeft

layout_goneMarginTop

layout_goneMarginRight

layout_goneMarginBottom

即使控件设置为gone,margin值仍然存在。

 

4、尺寸约束:

constraintlayout常见的问题:

使用wrap_content,让控件自己计算大小,当控件的高度或宽度为wrap_content时,

可以使用下列属性来控制最大、最小的高度或宽度,以宽度为例:

android:minWidth 最小的宽度

android:minHeight 最小的高度

android:maxWidth 最大的宽度

android:maxHeight 最大的高度

app:layout_constrainedWidth="true”属性。是为了防止左右条件约束的失效,例如:

textview的内容不断增多,导致内容挤出屏幕显示不出,就会导致左右约束的失效。

 

5、百分比布局:

当宽或高至少有一个尺寸被设置为0dp时,可以通过属性layout_constraintDimensionRatio设置宽高比,除此之外,在设置宽高比的值的时候,还可以在前面加W或H,分别指定宽度或高度限制。 例如:

app:layout_constraintDimensionRatio="H,2:3"指的是 高:宽=2:3

app:layout_constraintDimensionRatio="W,2:3"指的是 宽:高=2:3

 

ConstraintLayout还提供了另外一种偏移的属性:

layout_constraintHorizontal_bias 水平偏移

layout_constraintVertical_bias 垂直偏移

范围为 0-1 的值:假如赋值为0,在布局的最右侧,假如假如赋值为0.5,则水平居中,假如假如赋值为0.3,则更倾向于左侧。垂直偏移同理。

app:layout_constraintHorizontal_weight=“1"

控件在水平方向上的权重设置。

注意:水平权重,宽度约束设置为0dp,竖直权重,高度约束设置为0dp。

 

6、Guideline

Guideline 与 LinearLayout 类似可以设置水平或垂直方向,android:orientation="horizontal",android:orientation="vertical",水平方向高度为0,垂直方向宽度为0。Guideline 具有以下的三种定位方式:

  • layout_constraintGuide_begin 距离父容器起始位置的距离(左侧或顶部)

  • layout_constraintGuide_end 距离父容器结束位置的距离(右侧或底部)

  • layout_constraintGuide_percent 距离父容器宽度或高度的百分比

 

7、Barrier

barrierDirection 指定方向,constraint_referenced_ids引用的控件 id(多个id以逗号隔开)。

app:constraint_referenced_ids="TextView1,TextView2" />

注意:使用时要进行互相约束(指的是Barrier左右或者上下两边的控件都要对barrier进行约束才有效)。

8、Group

可以把多个控件归为一组,方便隐藏或显示一组控件

app:constraint_referenced_ids="TextView3,TextView4" />

9、Chains

条件:一组控件它们通过一个双向的约束关系链接起来。一条链的第一个控件是这条链的链头,我们可以在链头中设置 layout_constraintHorizontal_chainStyle来改变整条链的样式。

 注意:链的属性是由一条链的头结点控制的

注意:如果布局的没有剩余空间已经被填充满设置Chains是无效的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值