Percents

允许View在布局中占用的对应的比例,让宽度去适配比例,layout_width = 0dp,并且需要用 layout_constraintEnd_toEndOflayout_constraintStart_toStartOf 限定左右的边界,最后才用layout_constraintWidth_percent 表示在边界范围内占的比例情况,例子如下:

  1. <Button
  2. android:layout_width="0dp"
  3. android:layout_height="wrap_content"
  4. android:text="70% width"
  5. app:layout_constraintEnd_toEndOf="parent"
  6. app:layout_constraintStart_toStartOf="parent"
  7. app:layout_constraintWidth_percent="0.7"/>

Chains

链式风格的布局,只有元素>=3个时才会生效,分为 layout_constraintHorizontal_chainStylelayout_constraintVertical_chainStyle 两种链式风格,分别有 spread_inside, spread, packed 三种布局,且只需要对第一个元素 设置即可,其他设置与否无任何影响。无论在哪个方向,都需要针对parent有边界限制,如下:
ConstraintLayout 1.1 - 图1

  1. <Button
  2. android:id="@+id/chains1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="chains1"
  6. app:layout_constraintHorizontal_chainStyle="spread_inside" // 在此第一个设置即可
  7. app:layout_constraintLeft_toLeftOf="parent" // 左边界,要不统一成 start_toStartOf模式
  8. app:layout_constraintRight_toLeftOf="@+id/chains2" // 右链 chains2
  9. app:layout_constraintTop_toBottomOf="@+id/percents"/>
  10. <Button
  11. android:id="@+id/chains2"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="chains2"
  15. app:layout_constraintLeft_toRightOf="@id/chains1" // 左链 chains1
  16. app:layout_constraintRight_toLeftOf="@id/chains3" // 右链 chains3
  17. app:layout_constraintTop_toBottomOf="@+id/percents"/>
  18. <Button
  19. android:id="@+id/chains3"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="chains3"
  23. app:layout_constraintLeft_toRightOf="@id/chains2" // 左链 chains2
  24. app:layout_constraintRight_toRightOf="parent" // 左边界,要不统一成 end_toEndOf
  25. app:layout_constraintTop_toBottomOf="@+id/percents"/>�

Barrier

定义某些元素的边界,主要是 Barrier 类的使用,定义了 app:barrierDirection 方向(left,start,top,right,end,bottom),以及 app:constraint_referenced_ids 关联相应view的 id
例如将 给 textView1, textView2定义右边界barrier, textView3 根据右边界限制缩放:
ConstraintLayout 1.1 - 图2

  1. <TextView
  2. android:id="@+id/textView1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:layout_marginStart="16dp"
  6. android:layout_marginTop="16dp"
  7. android:text="warehouse"
  8. app:layout_constraintStart_toStartOf="parent"
  9. app:layout_constraintTop_toBottomOf="@id/chains1" />
  10. <TextView
  11. android:id="@+id/textView2"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_marginStart="16dp"
  15. android:layout_marginTop="8dp"
  16. android:text="hospital"
  17. app:layout_constraintStart_toStartOf="parent"
  18. app:layout_constraintTop_toBottomOf="@+id/textView1" />
  19. <android.support.constraint.Barrier
  20. android:id="@+id/barrier"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. app:barrierDirection="end"
  24. app:constraint_referenced_ids="textView2,textView1" />
  25. <TextView
  26. android:id="@+id/textView3"
  27. android:layout_width="0dp"
  28. android:layout_height="wrap_content"
  29. android:layout_marginLeft="8dp"
  30. android:layout_marginStart="8dp"
  31. android:text="@string/lorem"
  32. app:layout_constraintStart_toEndOf="@+id/barrier"
  33. app:layout_constraintTop_toBottomOf="@id/chains2"/>�

Group

在不增加view层级的前提下,对view进行 tag边界,做统一的操作,只需要通过app:constraint_referenced_ids配置需要组合的id,如下代码只需要通过 barrier_group 来控制整个group中相关的元素

  1. <android.support.constraint.Group
  2. android:id="@+id/barrier_group"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. app:constraint_referenced_ids="textView3,textView2,textView1"
  6. />�

Circular Constraints

以往的 ConstraintLayout 都是在 Horizontal 和 Vertical 方向对元素进行限制,1.1 新增了 layout_constraintCircleRadiuslayout_constraintCircleAngle通过 angleradius 在一个圆轨迹上做限制。 角度是从顶部开始,且顺时针布局, 其他元素只需要通过 layout_constraintCircle 执行中心元素的id即可。
ConstraintLayout 1.1 - 图3

  1. <Button
  2. android:id="@+id/center"
  3. android:layout_width="50dp"
  4. android:layout_height="50dp"
  5. android:text="O"
  6. app:layout_constraintTop_toBottomOf="@+id/textView3"
  7. app:layout_constraintEnd_toEndOf="parent"
  8. app:layout_constraintBottom_toBottomOf="parent"
  9. app:layout_constraintCircleRadius="50dp"
  10. app:layout_constraintCircleAngle="315"
  11. />
  12. <Button
  13. android:layout_width="50dp"
  14. android:layout_height="50dp"
  15. android:text="A"
  16. app:layout_constraintCircleRadius="100dp"
  17. app:layout_constraintCircle="@+id/center"
  18. app:layout_constraintCircleAngle="315"
  19. />
  20. <Button
  21. android:layout_width="50dp"
  22. android:layout_height="50dp"
  23. android:text="B"
  24. app:layout_constraintCircleRadius="100dp"
  25. app:layout_constraintCircle="@+id/center"
  26. app:layout_constraintCircleAngle="270"
  27. />
  28. <Button
  29. android:layout_width="50dp"
  30. android:layout_height="50dp"
  31. android:text="C"
  32. app:layout_constraintCircleRadius="100dp"
  33. app:layout_constraintCircle="@+id/center"
  34. app:layout_constraintCircleAngle="225"
  35. />�

Bias(1.0)

左右、上下约束布局

  1. app:layout_constraintEnd_toEndOf="parent"
  2. app:layout_constraintStart_toStartOf="parent"
  3. app:layout_constraintHorizontal_bias="0.5"

�上述代码设置了左右约束布局,因此Horizontal_bias有效,且 0.5表示居中,默认就是0.5
上下约束布局同理如下:

  1. app:layout_constraintBottom_toBottomOf="parent"
  2. app:layout_constraintTop_toTopOf="parent"
  3. app:layout_constraintVertical_bias="0.5"