Android开发之ConstraintLayout布局

164 阅读1分钟

ConstraintLayout 是用来替代传统的RelativeLayout布局,可以说是LinearLayout+RelativeLayout+FrameLayout的组合。基本上可以实现所有复杂和嵌套的布局,而不用使用布局嵌套,从此杜绝布局嵌套噩梦。

要使用ConstraintLayout布局要引入依赖库:

dependencies {
    ...
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    ...
}

阿斯蒂芬撒旦发射点asdfdsaf

啊手动阀手动阀

androidx.constraintlayout.widget.ConstraintLayout

基本用法

对齐

<?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!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • app:layout_constraintBottom_toBottomOf="parent" 表示TextView控件的底部与其父控件的底部对齐
  • app:layout_constraintLeft_toLeftOf="parent" 表示TextView控件的左边与其父控件的左边对齐
  • app:layout_constraintRight_toRightOf="parent" 表示TextView控件的右边与其父控件的右边对齐
  • app:layout_constraintTop_toTopOf="parent" 表示TextView控件的顶部与其父控件的顶部对齐

这四个属性都加上最终的效果是TextView居中显示。如图

image.png

平均排列

有时候需要多个控件水平或垂直方向等宽排列,则可以使用类似LinearLayout里的weight属性。

<?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="TextView 1"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/text2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView 2"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@id/text1"
        app:layout_constraintRight_toLeftOf="@id/text3"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView 3"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@id/text2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

效果图:

image.png

比例

使用 app:layout_constraintHorizontal_weight属性控制水平方向的控件占比。

<?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="0dp"
        android:layout_height="wrap_content"
        android:text="TextView 1"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/text2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView 2"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintLeft_toRightOf="@id/text1"
        app:layout_constraintRight_toRightOf="@id/text3"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView 3"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@id/text2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

多个控件水平方向均匀分布

<?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="0dp"
        android:layout_height="wrap_content"
        android:text="TextView 1"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/text2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView 2"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@id/text1"
        app:layout_constraintRight_toRightOf="@id/text3"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView 3"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@id/text2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

阿斯蒂芬撒地方

高级用法

啊手动阀手动阀

花式用法

啊手动阀