1.ConstraintLayout介绍

1:ConstraintLayout也称为约束布局,它的出现为了减少布局的嵌套,从而提高视图绘制的性能。同时ConstraintLayout也可以实现简单的动画效果。
2:ConstraintLayout在Android Studio2.3中被使用,作为创建Empty Activity的默认布局。

2.app:layout_constraintXxx_toXxxOf系列属性-12个

1:这一系列的12个属性主要是进行相对布局,主要有六组Top、Bottom、Left、Right、Start、End,每组有两个。如app:layout_constraintTop_toTopOf。其中Start、End和Left、Right的区别是,Left必然从左边开始布局,而Start则会考虑使用的国度。
2:属性值主要有两种。parent,表示相对于父控件的布局。@id/xx,相对于兄弟空间的布局。
3:在使用的使用要注意,ConstraintLayout中的每一子控件。上下方向上Top或者Bottom必须有一个进行上下方向上的位置控制。同样左右必须有Right、Left、Start、End中的一个,进行左右位置的控制。这样该子控件就知道自己要显示的位置。
4:ConstraintLayout中,如果要使用layout_marginXxx系列的属性,如layout_marginTop,就必须使用layout_constraintTop_toTopOf约束顶部的位置,不然layout_marginTop无效。
5:对于3约束中的解释。ConstraintLayout和RelativeLayout不同,我们知道对于RelativeLayout,它的子控件只要设置了layout_width、layout_height就可以进行显示,
因为其默认会从RelativeLayout的左上开始布局。而对于ConstraintLayout,如果只设置了layout_width、layout_height子空间不知道自己从何处布局,所以要给定左右、上下的约束。如果只给定上下方向的一个约束,如layout_constraintTop_toTopOf="parent",
子控件就显示在上方,可以理解为上方对他产生了一个拉力,如果在加上layout_constraintBottom_toBottomOf="parent",则下方也对他产生了拉力,则它会显示在上下的中间。左右同理。
6:对于4的理解,基于5。同样如果使用layout_marginTop,而没有使用layout_constraintTop_toTopOf,则会优先匹配layout_constraintBottom_toBottomOf,从而忽略layout_constraintTop_toTopOf。

3.app:layout_constraintXxx_creator系列-4个

layout_constraintXX_creator系列,在可视化编辑其中使用。

4.app:layout_goneMarginXxx系列-6个

layout_goneMarginXxx系列有Bottom、End、Left、Right、Start、Top,这些属性会在参照物是gone的时候生效。

5.app:ayout_constraint[Width|Height]-宽高约束

1:宽的最大、最小的约束。layout_constraintWidth_min、layout_constraintWidth_max。
2:高的最大、最小的约束。layout_constraintHeight_min、layout_constraintHeight_max。
3:宽高按照父布局的百分比进行约束。layout_constraintWidth_percent、layout_constraintHeight_percent。值为小数。
4:宽高是否进行约束。layout_constrainedWidth、layout_constrainedHeight。值是boolean。使用的时机,layout_width="wrap_content",
layout_constraintWidth_min="200dp",此时最小的宽不会生效,需要使用layout_constrainedWidth="true"进行约束后才会生效。
5:默认宽高。layout_constraintWidth_default、layout_constraintHeight_default,一般不会使用。有三个值spread、warp的都和layout_width="wrap_content"效果一样,
但是warp已经弃用。percent和layout_constraintWidth_percent="1"效果一样。

5.app:layout_constraint[Horizontal|Vertical]-水平和垂直约束

1:layout_constraint[Horizontal|Vertical]_bias,接着上面2第3点说,同方向当有两个拉力是,在中间。而bias默认是0.5,大于往下、右,小于往上、左。
2:layout_constraint[Horizontal|Vertical]_chainStyle、layout_constraint[Horizontal|Vertical]_weight,相当于LinearLayout时,layout_width="0dp",
而layout_weight设置权重。chainStyle使其处在同一条线上、weight设置权重。

6.app:layout_constraintDimensionRatio设置宽高比

layout_constraintDimensionRatio,值可以是"H,20:9"或者"W,9:20",在使用这个属性的时候,
应该将宽或者高一个设置为"0dp",另一个基于这个固定的进行比例设置。

7.app:layout_editor_absoluteX|Y

app:layout_editor_absoluteX|Y进行控件的X、Y轴的绝对定位,不经常使用。

8.Guideline

1:Guideline只会在xml绘图的时候显示,作为辅助线,不会在实际中展示。
2:Guideline的用法。
<androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="100dp"/>
3:通过使用2中的12个属性让其他控件和Guideline对齐。

9.Barrier

<androidx.constraintlayout.widget.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">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名:"
        android:textSize="30sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码:"
        android:textSize="30sp"
        app:layout_constraintTop_toBottomOf="@id/tv1"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="right"
        app:constraint_referenced_ids="tv1,tv2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

10.Group、Placeholder

1:Group可以控制过个控件的可见性,使用visibility="visible"控制可见性,使用constraint_referenced_ids确定要控制的空间。
2:Placeholder占位符,通过设置content="指定控件的id",来使得这个控件移动到占位符的位置去。