Android ConstraintLayout中的页边距行为

Android ConstraintLayout中的页边距行为,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,在ConstraintLayout中使用页边距时,我很难理解页边距的行为(android:layout\u margin*)。保证金似乎只在某些情况下有效,我希望有人能向我解释一下(或者确认这是一个限制性缺陷) 我有下面的布局 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.c

在ConstraintLayout中使用页边距时,我很难理解页边距的行为(
android:layout\u margin*
)。保证金似乎只在某些情况下有效,我希望有人能向我解释一下(或者确认这是一个限制性缺陷)

我有下面的布局

<?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>
。。。空白突然消失了


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

因为
rightView
依赖于其父级(
ConstraintLayout
),所以您可以设置:

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
父级
ConstraintLayout
。您这样做是因为您使
rightView
位于其父视图的右上角(
ConstraintLayout

因此,您在
右视图中使用了
android:layout_margintart=“20dp”
rightView
将与其父视图(
ConstraintLayout
)保持左侧的
20dp
。所以在这种情况下没有空格


第二种情况与第一种情况不同。查看
左视图

leftView
始终位于
rightView
的左侧,因为您在
leftView
中使用了
app:layout\u constraintEnd\u toStartOf=“@+id/rightView”

由于您在
左视图中使用了以下代码,因此
左视图
也依赖于其父视图(ConstraintLayout)

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/leftView"
您在
leftView
中使用了
android:layout\u marginEnd=“20dp”
,这意味着
leftView
将在
rightView
中右边距
20dp
。所以在这个例子中有空白



您可以尝试使用
app:layout\u constraintEnd\u toEndOf=“parent”
来查看
leftView
,您将看到不同的视图。

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

当视图有边距时,表示该视图提供了视图所依赖的空间


如果您在
rightview
中编写
android:layout_margintart=“20dp”
,它将不会给
leftview
留出空间,因为
rightview
不是依赖于
leftview
要获得您想要的结果,您需要构建一个
链。如果定义了链的组件之间的所有链接,则链是正确定义的

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

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/leftView"
并将leftView中现有的
布局\u constraint倾向\u toStartOf
约束更改为

app:layout_constraintRight_toLefttOf="@+id/rightView"

好奇:如果您通过
app:layout\u constraints start\u toEndOf=“@+id/rightView”
app:layout\u constraints start\u toEndOf=“@+id/leftView”
”移动到
rightView
,会发生什么情况?@stkent的行为与第二种情况几乎相同。但是在这种情况下,
LeftView
将填充整个屏幕宽度,除非
LayoutWidth
设置为某个值,如
100dp
。我也不明白这种行为。谢谢。这可能是对这个问题最清晰、最简洁的解释,但它不是很直观。我有一个视图,我想将页边距设置为20dp,底部页边距不应用,因为下面的对象在该视图下面有一个要定位的约束。在发布之前,我们很自然地认为边距是视图空间的一部分。