ConstraintLayout中的保证金行为

Gus*_*Gus 7 android android-layout android-constraintlayout

android:layout_margin*在ConstraintLayout中使用时,我很难理解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>
Run Code Online (Sandbox Code Playgroud)

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

预期产量

但是,当我将边距从更改leftViewrightView...

<?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> 
Run Code Online (Sandbox Code Playgroud)

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

意外的输出

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

zih*_*yef 5

因为leftview是依赖的rightview,所以您可以设置余量leftviewrightview

当视图具有边距时,表示视图正在给视图依赖的空间。

如果您android:layout_marginStart="20dp"输入rightview,它不会给空格,leftview因为rightview它不依赖leftview