ConstraintLayout 中的边距行为

Margin behavior in ConstraintLayout

我无法理解在 ConstraintLayout 中使用边距 (android:layout_margin*) 的行为。边距似乎只在某些情况下生效,我希望有人能给我解释一下(或者确认这是一个 ConstraintLayout 错误)。

我有以下布局...

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
         android:id="@+id/leftView"
         android:layout_width="0dp"
         android:layout_height="100dp"
         android:layout_marginEnd="20dp"
         android:background="@android:color/holo_blue_dark"
         app:layout_constraintEnd_toStartOf="@+id/rightView"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent"/>

     <View
         android:id="@+id/rightView"
         android:layout_width="100dp"
         android:layout_height="100dp"
         android:background="@android:color/holo_green_light"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

...产生以下输出...

但是,当我将边距从 leftView 更改为 rightView...

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/leftView"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_blue_dark"
        app:layout_constraintEnd_toStartOf="@+id/rightView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <View
        android:id="@+id/rightView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="20dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout> 

...边距意外消失...

有人可以解释这是否是预期的行为,如果是,为什么会这样?

因为 rightView 依赖于它的父级 (ConstraintLayout),所以你设置:

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"

parentConstraintLayout。你做到了,你让 rightView 位于其父项 (ConstraintLayout)

的右上角

所以你在rightView中使用了android:layout_marginStart="20dp"rightView 将在其父项 (ConstraintLayout) 的左边距 20dp。所以本例中没有白色space。


第二种情况与第一种情况不同。查看 leftView

leftView 始终在 rightView 的左侧,因为您在 leftView

中使用了 app:layout_constraintEnd_toStartOf="@+id/rightView"

AND leftView 也取决于其父级(ConstraintLayout),因为您在 leftView

中使用了以下代码
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"

AND 你在 leftView 中使用了 android:layout_marginEnd="20dp" 这意味着 leftView 将在 20dp 右边距 rightView。所以在这种情况下有白色space。


您可以尝试使用 app:layout_constraintEnd_toEndOf="parent" 作为 leftView 您会看到不同的结果。

因为leftview依赖于rightview,所以可以将leftview的边距设置为rightview

当一个视图有边距时,这意味着该视图正在提供 space 视图所依赖的对象

如果你在 rightview 中写 android:layout_marginStart="20dp",它不会把 space 给 leftview 因为 rightview 不是 DEPENDINGleftview.

要获得您正在寻找的结果,您需要构建一个 chain。如果定义了链组件之间的所有链接,则正确定义了链。

在您的代码中,rightView 必须连接到 leftView。将以下约束添加到 rightView

app:layout_constraintLeft_toRightOf="@+id/leftView"

并将 leftView 中现有的 layout_constraintEnd_toStartOf 约束更改为

app:layout_constraintRight_toLefttOf="@+id/rightView"