一、简介
ConstraintLayout具有很好的性能优势,可以绘制扁平化界面,减少复杂界面造成的布局嵌入过深的问题,经常使用一层嵌套便可以实现界面。
ConstraintLayout是使用比例方式进行的排版布局,所以可以很好的适配各种大小和分辨率的手机屏幕。
二、使用
1.如何在代码中引入ConstraintLayout
新建一个布局文件可以看到,Google已经默认帮我们选择好了父布局为ConstraintLayout。
2.ConstraintLayout布局的约束属性
ConstraintLayout共给我们提供了13种属性,通过属性的意思便能知道属性的具体含义。
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
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf
下面通过以上属性实现一个九宫格界面布局的例子,直接放代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout ...>
<TextView
android:id="@+id/tv_1"
android:layout_width="0dp"
android:layout_height="0dp"
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@id/tv_4"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tv_2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_2"
android:layout_width="0dp"
android:layout_height="0dp"
...
app:layout_constraintBottom_toTopOf="@id/tv_5"
app:layout_constraintLeft_toRightOf="@id/tv_1"
app:layout_constraintRight_toLeftOf="@id/tv_3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_3"
android:layout_width="0dp"
android:layout_height="0dp"
...
app:layout_constraintBottom_toTopOf="@id/tv_6"
app:layout_constraintLeft_toRightOf="@id/tv_2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_4"
android:layout_width="0dp"
android:layout_height="0dp"
...
app:layout_constraintBottom_toTopOf="@id/tv_7"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tv_5"
app:layout_constraintTop_toBottomOf="@id/tv_1" />
<TextView
android:id="@+id/tv_5"
android:layout_width="0dp"
android:layout_height="0dp"
...
app:layout_constraintBottom_toTopOf="@id/tv_8"
app:layout_constraintLeft_toRightOf="@id/tv_4"
app:layout_constraintRight_toLeftOf="@id/tv_6"
app:layout_constraintTop_toBottomOf="@id/tv_2" />
<TextView
android:id="@+id/tv_6"
android:layout_width="0dp"
android:layout_height="0dp"
...
app:layout_constraintBottom_toTopOf="@id/tv_9"
app:layout_constraintLeft_toRightOf="@id/tv_5"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_3" />
<TextView
android:id="@+id/tv_7"
android:layout_width="0dp"
android:layout_height="0dp"
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tv_8"
app:layout_constraintTop_toBottomOf="@id/tv_4" />
<TextView
android:id="@+id/tv_8"
android:layout_width="0dp"
android:layout_height="0dp"
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/tv_7"
app:layout_constraintRight_toLeftOf="@id/tv_9"
app:layout_constraintTop_toBottomOf="@id/tv_5" />
<TextView
android:id="@+id/tv_9"
android:layout_width="0dp"
android:layout_height="0dp"
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/tv_8"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_6" />
</androidx.constraintlayout.widget.ConstraintLayout>
上述约束实现的布局效果如下:
3.ConstraintLayout布局居中
ConstraintLayout可以根据比例进行页面的调整,用到的属性有以下几个:
layout_constraintHorizontal_bias //水平方向居中时左右边距的比例
layout_constraintVertical_bias //垂直方向居中时上下边距的比例
通过一个例子可以很直观的看到:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout ... >
<TextView
android:id="@+id/tv_1"
android:layout_width="100dp"
android:layout_height="100dp"
...
app:layout_constraintBottom_toTopOf="@id/tv_2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/tv_2"
android:layout_width="100dp"
android:layout_height="100dp"
...
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_1"
app:layout_goneMarginBottom="20dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
运行结果:
4.角度定位
ConstraintLayout可以通过三个属性,实现按照中心点控件进行的角度定位。
app:layout_constraintCircle //约束以谁为圆点
app:layout_constraintCircleRadius //约束角度的距离
app:layout_constraintCircleAngle //约束的角度
以下代码为让蓝色圆圈围绕红色圆点顺时针旋转123度:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout ...>
<TextView
android:id="@+id/tv_center"
android:layout_width="100dp"
android:layout_height="100dp"
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintCircle="@id/tv_center"
app:layout_constraintCircleAngle="123"
app:layout_constraintCircleRadius="200dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
运行结果: