Android ConstraintLayout中的页边距行为
在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
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,底部页边距不应用,因为下面的对象在该视图下面有一个要定位的约束。在发布之前,我们很自然地认为边距是视图空间的一部分。