ConstraintLayout —— 约束布局 知识点整理

版权声明:转载请标明出处「OneDeveloper」 https://blog.csdn.net/OneDeveloper/article/details/82021197

参考文章:
1、Android官方文档
2、约束布局(ConstraintLayout)1.1.2 版本的新特性
3、android ConstraintLayout使用详解
4、Android ConstraintLayout+ConstraintSet实现动画效果


ConstraintLayout 的版本是 1.1.x


layout_constraintXXX_toYYYOf 系列

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

如对控件 A 使用 layout_constraintLeft_toRightOf = “@id/B“ 则表示 A 对左边边界对齐 B 对右边边界。

其中,有类似于如下的情形,那就是app:layout_constraintStart_toStartOf
app:layout_constraintLeft_toLeftOf用起来感觉没啥区别,但是存在即合理,为啥要有两种形式的存在呢?
答案是为了 Android中的RTL Layout 兼容,具体可以看这儿

Bias

layout_constraintHorizontal_bias
layout_constraintVertical_bias

Guideline

layout_constraintGuide_begin
layout_constraintGuide_end
layout_constraintGuide_percent

以及实现控件的宽高比
app:layout_constraintDimensionRatio
当宽高至少有一项被设置为 0dp 时,可以使用属性 layout_constraintDimentionRatio来给定一个”宽对高”的比率。比率值是一个float值,可以写成 0.5 或 1:2 这样的形式。

类似于 LinearLayout 的 weight 的实现

app:layout_constraintHorizontal_weight
app:layout_constraintVertical_weight

约束链

app:layout_constraintHorizontal_chainStyle
app:layout_constraintVertical_chainStyle

可以为 Chain 中的每个子 View 单独设置 Margin。对于 spread chains, 可用的布局空白空间是扣除 margin 后的空间。

都可以参阅【鸿神的博客:https://blog.csdn.net/lmj623565791/article/details/78011599


Margins
原本的 android:layout_marginXXX 系列。不多说。

需要注意的是 View 的四个 margin 值也与 View 的最终位置有关,如果设置了某个方向的 margin 值,那么只有设置相对应方向的约束条件,这个方向的 margin 值才生效。比如设置了android:layout_marginStart="20dp",那么当且仅当设置了 start 的约束条件app:layout_constraintStart_toStartOf或者app:layout_constraintStart_toEndOf这个 margin 值才生效。还要注意的是ConstraintLayout不支持负数的 margin,如果是负数的话效果和 0 一样的,当然负数的 padding 是没问题的。
【引自:https://juejin.im/post/5b26844a6fb9a00e9e59c8a2

以及 layout_goneMarginXXX 系列,作用是控制当前 View 所参考的 View 状态为 GONE 的时候的 margin 值,如果不为 GONE 则不起作用。

app:layout_goneMarginLeft 为例,假设控件 B 相对在 A 的右边,如图:
这里写图片描述

<TextView
    android:id="@+id/tvA"
    android:visibility="visible"
    ... />
<TextView
    android:id="@+id/tvB"
    app:layout_goneMarginLeft="50dp"
    app:layout_constraintLeft_toRightOf="@id/tv1"
    ... />

并且为 B 设置了 app:layout_goneMarginLeft="50dp",此时如果 A 为 GONE,那么B 将距离屏幕的左边 50dp。如图:
这里写图片描述


app:layout_constrainedWidth\Height 与控件大小
android:maxXXX\minXXXapp:layout_constrainedWidth_max等 都是用来限制 View 的最大\最小的宽高的。

app:layout_constrainedWidth
app:layout_constrainedHeight

app:layout_constraintWidth_max
app:layout_constraintWidth_min
app:layout_constraintHeight_max
app:layout_constraintHeight_min

1.1 版本之前,如果将控件的尺寸设置为 wrap_content,那么对控件设置的 app:layout_constrainedWidth\Height这些约束是不起作用的,因此就在 1.1 中增加了
app:layout_constrainedWidth 、app:layout_constrainedHeight 这两个属性,当置为 true 时且控件尺寸为 wrap_content 时,通过 app:layout_constrainedWidth_max 等设置等限制大小的值就会生效。

另外,当 layout_width 或者 layout_heightwrap_content 时(注意:非 0dp),同样可以使用对应的 android:minXXXandroid:maxXXX 来限制控件的尺寸。

而如果有同时设置 android:maxHeightapp:layout_constrainedWidth_max,则不管有没有设置 app:layout_constrainedWidth,都会强制生效 android:maxHeight ,而忽略app:layout_constrainedWidth_max的影响。

【参考博客:https://juejin.im/post/5b26844a6fb9a00e9e59c8a2


圆形定位 —— Circular positioning (Added in 1.1)

layout_constraintCircle : 被引用控件的 id
layout_constraintCircleRadius : 目标控件中心距离被引用控件中心的距离
layout_constraintCircleAngle : 相对于被引用控件中心的角度(单位:度,范围:0360

这里写图片描述


宽高类

app:layout_constraintWidth_default
app:layout_constraintHeight_default

app:layout_constraintWidth_default只有在View的宽度定义为0dp(又叫match_constraint)的时候才生效,其余情况下设置这个属性是不起任何作用的,有三个值:

wrap:等价于 android:layout_width=”wrap_content”
spread:等价于 android:layout_width=”match_parent”
percent:设置 View 的宽度为 parent 的比例值,比例值默认是100%,即宽度是match_parent。这个比例值通过属性app:layout_constraintWidth_percent设置。

如果设置了 app:layout_constrainWidth_percent
app:layout_constraintWidth_default 失效。

根据父布局的大小,实现子控件的尺寸百分比占比

app:layout_constrainWidth_percent
app:layout_constrainHeight_percent

比如需要使用 app:layout_constrainHeight_percent
则需要设置 android:layout_height 为 0dp(其余不生效),且要设置

app:layout_constraintTop_toXXXOf
app:layout_constraintBottom_toXXXOf

来约束控件的 Top 和 Bottom。

Placeholder
(占位符)用于和一个视图关联起来,通过 setContentId() 方法将占位符转换为指定的视图,即视图将在占位符所在位置上显示,如果此时布局中已包含该视图,则视图将从原有位置消失。

【参阅博文:https://juejin.im/post/5b26844a6fb9a00e9e59c8a2


Barrier
是一个辅助类,不会绘制到屏幕上,也不会展现给用户。它通过属性constraint_referenced_ids将一些View包裹在一起形成一个屏障,然后通过属性barrierDirection向左上右下四个方向给某个 View 提供约束条件,或者叫做屏障方向。

使用这些约束条件(屏障方向)的 View A**可以防止屏障内的 Views 覆盖自己,当屏障内的某个 View C** 要覆盖自己的时候,包含 C 的屏障会自动伸缩,依赖屏障约束的 A 因此也会跟着移动,避免自己被覆盖。

Group
也是一个辅助类,不会绘制到屏幕上,也不会展现给用户。通过属性app:constraint_referenced_ids 将一些 View 组成组进行集体操作,最常见的操作是setVisibility

<android.support.constraint.Group
        android:id="@+id/group1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="button3, textView2"/>

Group 设置的宽高是不起作用的。

【参阅博文:https://www.jianshu.com/p/f86f800964d2


ConstraintSet 与 TransitionManager 实现动画
【参阅博文:https://blog.csdn.net/weixin_37730482/article/details/71080567


额外的非必要属性

下面几个属性是 UI 编辑器所使用的,用了辅助拖拽布局的,在实际使用过程中,可以不用关心这些属性。
● layout_optimizationLevel
● layout_editor_absoluteX
● layout_editor_absoluteY
● layout_constraintBaseline_creator
● layout_constraintTop_creator
● layout_constraintRight_creator
● layout_constraintLeft_creator
● layout_constraintBottom_creator

【参阅博客:http://blog.chengyunfeng.com/?p=1030#ixzz5P4qMkNW7


附:Android Space一个轻量级的创造空隙的控件

猜你喜欢

转载自blog.csdn.net/OneDeveloper/article/details/82021197