扁平式布局
因为嵌套的层次结构会导致性能低下,所以ConstraintLayout就采用了扁平的视图层次结构
主要属性
- layout_constraintRight_toLeftOf
- layout_constraintRight_toRightOf
- layout_constraintTop_toTopOf
- layout_constraintTop_toBottomOf
- layout_constraintBottom_toTopOf
- layout_constraintBottom_toBottomOf
- layout_constraintBaseline_toBaselineOf
使该控件的左侧与父布局对齐
app:layout_constraintLeft_toLeftOf="parent"
使该控件在控件B的右侧
app:layout_constraintLeft_toRightOf="@id/viewB"
将控件的width设置为0:代表match_constraint(相当于match_parent)
宽高比
app:layout_constraintDimensionRatio="16:6"
//或者
app:layout_constraintDimensionRatio="W,16:6"
app:layout_constraintDimensionRatio="H,16:6"
设置权重
app:layout_constraintHorizontal_weight
居中方式
- 父容器的居中
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
- 同一层View的居中
app:layout_constraintLeft_toLeftof="id"
app:layout_constraintRight_toRightof="id" //这两行水平居中
app:layout_constraintTop_toTopOf="id"
app:layout_constraintBottom_toBttomOf="id" //这两行垂直居中
- 同一层View的边界居中
app:layout_constraintTop_toBottomOf="id"
app:layout_constraintBottom_toBttomOf="id" //居中在另一个控件底部边界