1.简介
ConstraintLayout是一个ViewGroup,翻译为约束布局,约束布局的出现主要是为了解决布局嵌套过深的情况,自android studio2.3开始,新建布局文件默认根ViewGroup就是它
2.基本属性
2.1 相对定位
例如TextView2需要放在TextView1后方,可以使用app:layout_constraintStart_toEndOf
Start_toEndOf
android:id="@+id/txt1"
android:text="Text1"/>
android:id="@+id/txt2"
android:text="Text2"
app:layout_constraintStart_toEndOf="@id/txt1"/>
相似的属性有:
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:对TextView有效,基线对齐
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf
如果是依赖父布局的话可以使用parent
app:layout_constraintStart_toEndOf="parent"
2.2间距margins
margin和其它布局方式一样,支持的属性有:
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
但是ConstraintLayout多了一种间距goneMargin,用来指定当依赖的控件visibility为gone时的间距,此属性当且仅当依赖控件