约束布局ConstraintLayout笔记

下面8个属性中,constraint后面的Left、Right、Top、Bottom是指View自己的左边还是右边,是上面还是下面;to后面的Left、Right、Top、Bottom是指目标View的左边还是右边,是上面还是下面;

可以这样理解:当前布局的View A的上/下/左/右边是在目标参照物View B的上/下/左/右边

layout_constraintLeft_toLeftOf:目标View的左边
layout_constraintLeft_toRightOf:目标View的右边
layout_constraintRight_toLeftOf:目标View的左边
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf

<TextView
    android:id="@+id/tvSubheading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="这里是副标题"
    android:padding="5dp"
    android:background="@drawable/btn_shape"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvTitle"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    android:layout_marginTop="15dp"
    android:layout_marginHorizontal="15dp"/>
在ConstraintLayout布局里面,margin属性是必须配合constraintxxx属性使用才会生效的
start和end与left和right对应。

layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

但居中则有些不一样:
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"

	第二个下划线前后表示方位的属性是一样的:
	...tLeft_toLeft...、...Top_toTop...、...Right_toRight...、...Bottom_toBottom...

layout_constraintBaseline_toBaselineOf:基线,是指View里面文本内容的基线,不是指View的对齐基线。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="试试BaseLine"
    android:background="@drawable/btn_shape"
    android:padding="10dp"
    app:layout_constraintLeft_toRightOf="@+id/tvLike"
    app:layout_constraintBaseline_toBaselineOf="@+id/tvLike"
    android:layout_marginStart="15dp" />

#方位布局三要素:基准、角度、距离
layout_constraintCircle——为View A指定参考基准View B(参数为B的id)
layout_constraintCircleAngle——指定View A在View B的什么方向(这里以角度来表示:xxx的整数)
layout_constraintCircleRadius——指定View A距离View B多远(xxxdp)

<TextView
    android:id="@+id/tvLike"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="点赞"
    android:textColor="@color/red"
    android:padding="15dp"
    android:background="@drawable/btn_shape"
    app:layout_constraintCircle="@id/tvTitle"
    app:layout_constraintCircleAngle="225"
    app:layout_constraintCircleRadius="120dp"/>

goneMargin主要用于约束的控件可见性被设置为gone的时候使用的margin值:
layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom

layout_constraintHorizontal_bias 水平偏移
layout_constraintVertical_bias 垂直偏移

app:layout_constraintHorizontal_bias=“0.3”
给layout_constraintHorizontal_bias赋一个范围为 0-1 的值,
假如赋值为0,则View在布局的最左侧;假如赋值为1,则View在布局的最右侧;
假如赋值为0.5,则水平居中;假如假如赋值为0.3,则更倾向于左侧;假如赋值为0.7,则更倾向于右侧
垂直偏移同理。

    <TextView
        android:id="@+id/tvSubheading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里是副标题"
        android:padding="5dp"
        android:background="@drawable/btn_shape"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvTitle"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.7"
        android:layout_marginTop="15dp"
        android:layout_marginHorizontal="15dp"/>

android:minWidth 最小宽度
android:maxWidth 最大宽度

android:minHeight 最小高度
android:maxHeight 最大高度

使用 0dp (MATCH_CONSTRAINT)官方不推荐在ConstraintLayout中使用match_parent,可以设置 0dp (MATCH_CONSTRAINT) 配合约束代替match_parent。如:

<TextView
    android:id="@+id/TextView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:visibility="visible" />

layout_constraintDimensionRatio设置宽高比,宽设置为0dp,宽高比设置为1:1,这个时候View是一个正方形
还可以在前面加W或H,分别指定宽度或高度限制。 例如:
app:layout_constraintDimensionRatio="H,2:3"指的是 高:宽=2:3
app:layout_constraintDimensionRatio="W,2:3"指的是 宽:高=2: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=".ConstraintLayoutActivity">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里设置标题"
        android:padding="5dp"
        android:background="@drawable/btn_shape"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/tvSubheading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里是副标题"
        android:padding="5dp"
        android:background="@drawable/btn_shape"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvTitle"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.7"
        android:layout_marginTop="15dp"
        android:layout_marginHorizontal="15dp"/>


    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里是内容"
        android:padding="5dp"
        android:background="@drawable/btn_shape"
        app:layout_constraintLeft_toRightOf="@+id/tvTitle"
        app:layout_constraintBottom_toTopOf="@+id/tvSubheading"
        android:layout_marginStart="15dp"/>

    <TextView
        android:id="@+id/tvLike"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点赞"
        android:textColor="@color/red"
        android:padding="15dp"
        android:background="@drawable/btn_shape"
        app:layout_constraintCircle="@id/tvTitle"
        app:layout_constraintCircleAngle="225"
        app:layout_constraintCircleRadius="120dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="试试BaseLine"
        android:background="@drawable/btn_shape"
        android:padding="10dp"
        app:layout_constraintLeft_toRightOf="@+id/tvLike"
        app:layout_constraintBaseline_toBaselineOf="@+id/tvLike"
        android:layout_marginStart="15dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述

原创文章 118 获赞 149 访问量 9万+

猜你喜欢

转载自blog.csdn.net/haoyuegongzi/article/details/104880850