依赖

  1. //推荐使用最新的代码
  2. implementation 'com.android.support.constraint:constraint-layout:x.x.x

1.相对定位

  • 一个View相对另外一个View的位置
  • constraintXXX表示View自身约束的边,toXXXOf表示另一个View的边,而XXX的值可以是Left、Top、Right、Bottom,分别对应左,上、右、下边。
  • 文本中间对齐:baseline

    2.边距

    常用边距

  • android:layout_marginLeft

  • android:layout_marginTop
  • android:layout_marginRight
  • android:layout_marginBottom

    GoneMargin

  • layout_goneMarginLeft

  • layout_goneMarginTop
  • layout_goneMarginRight
  • layout_goneMarginBottom

    3.居中和偏移(bias)

    居中

  • 水平居中:layout_constraintLeft_toLeftOf & layout_constraintRight_toRightOf

  • 垂直居中:layout_constraintTop_toTopOf & layout_constraintBottom_toBottomOf

    bias

  • 水平偏移:layout_constraintHorizontal_bias

  • 垂直偏移:layout_constraintVertical_bias

    两个属性的取值范围在0-1。在水平偏移中,0表示最左,1表示最右;在垂直偏移,0表示最上,1表示最下;0.5表示中间

4.圆形定位

  • layout_constraintCircle : 另外一个view的id
  • layout_constraintCircleRadius : 半径
  • layout_constraintCircleAngle : 角度

    圆形定位指的是View的中心点相对于另外View中心点的位置

5.尺寸约束

最大最小尺寸

  • android:minWidth 最小宽度
  • android:minHeight 最小高度
  • android:maxWidth 最大宽度
  • android:maxHeight 最大高度

    控件尺寸约束

  • 指定具体的值。如123dp

  • 使用值WRAP_CONTENT,内容自适配。
  • 设为0dp,即MATCH_CONSTRAINT,扩充可用空间。

    6.比例约束

  • layout_constraintDimensionRatio

    7.链

    横向链最左边第一个控件,垂直链最顶边第一个控件称为链头,可以通过下面两个属性链头统一定制链的样式

  • layout_constraintHorizontal_chainStyle 水平方向链式

  • layout_constraintVertical_chainStyle 垂直方向链式

    8.优化器

    9.工具类

    Guideline(参考线)

    Barrier(栅栏)

    Group(组)

    Placeholder(占位符)

    参考资料

    Android新特性介绍,ConstraintLayout完全解析 ConstraintLayout,看完一篇真的就够了么?