Android中ConstraintLayout布局的基本用法
约束布局(ConstraintLayout)是目前谷歌推荐的一种布局,它的功能强大。相对于LinearLayout和RelationLayout相比,它的用法更加灵活,同时代码量更小。下面介绍一下约束布局的简单用法。
例子1:最简单用法
app:layout_constraintStart_toStartOf="id"//该模块的左边和id模块的左边对齐
app:layout_constraintEnd_toStartOf="id"//该模块的右边和id模块的左边对齐
app:layout_constraintStart_toEndOf="id"//该模块的左边和id模块的右边对齐
app:layout_constraintEnd_toEndOf="id"//该模块的右边和id模块的右边对齐
app:layout_constraintTop_toTopOf="id"//该模块的上边和id模块的上边对齐
app:layout_constraintTop_toBottomOf="id"//该模块的上边和id模块的底边对齐
app:layout_constraintBottom_toTopOf="id"//该模块的底边和id模块的上边对齐
app:layout_constraintBottom_toBottomOf="id"//该模块的底边和id模块的底边对齐
前四条是横向的约束条件,后四条是纵向的约束条件,一个模块必须同时要有横向和纵向的约束条件。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent" //模块的左边和父布局的左边对齐
app:layout_constraintTop_toTopOf="parent" /> //模块的顶端和父布局的顶端对齐
</androidx.constraintlayout.widget.ConstraintLayout>
例子2:两模块相对布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/Text1" //设置ID
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm first"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm second"
android:textSize="20dp"
app:layout_constraintStart_toEndOf="@id/Text1" //Text2模块的左边和Text1模块的右边对齐
app:layout_constraintTop_toBottomOf="@id/Text1" /> //Text2模块的上边和Text1模块的底边对齐
</androidx.constraintlayout.widget.ConstraintLayout>

例子3:基线对齐
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm first"
android:textSize="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm second"
android:textSize="30dp"
app:layout_constraintStart_toEndOf="@id/Text1"
app:layout_constraintBaseline_toBaselineOf="@+id/Text1"/> //基线对齐
</androidx.constraintlayout.widget.ConstraintLayout>

例子4:角度约束
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm first"
android:textSize="50dp"
app:layout_constraintStart_toStartOf="parent" //左边与父布局左边对齐,右边与父布局右边对齐
app:layout_constraintEnd_toEndOf="parent" //那么它就在中间
app:layout_constraintTop_toTopOf="parent" />
<TextView
app:layout_constraintCircle="@id/Text1" //参考的模块
android:id="@+id/Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm second"
android:textSize="30dp"
app:layout_constraintCircleAngle="120" //距离
app:layout_constraintCircleRadius="120dp" //角度
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是ContriantLayout布局的基本用法,这些看着简单,但灵活运用起来并不是那么容易。有了约束布局,让页面设计更加灵活。
