ConstraintLayout中的边距行为
在ConstraintLayout中使用时,我无法理解边距的行为(android:layout_margin*
)。边距似乎只在某些情况下才起作用,我希望有人能向我解释(或确认它是一个ConstraintLayout错误)。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>
...保证金意外消失......
有人能解释这是否是预期的行为,如果是这样,这是为什么?
因为leftview
取决于rightview
,因此您可以将边距leftview
设置为rightview
。
当视图有余量时,这意味着视图给出的空间为视图取决于。如果你写在rightview
android:layout_marginStart="20dp"
,它不会leftview
给予空间,因为rightview
不是DEPENDING到leftview
。
谢谢。这可能是对这个问题最清楚和最简洁的解释。 – Gus
因为rightView
取决于它的父(ConstraintLayout
),您可以设置:
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
的parent
是ConstraintLayout
。你做了,你让rightView
位于其父母的右上方(ConstraintLayout
)
因此,您在rightView
中使用了android:layout_marginStart="20dp"
。 rightView
将在其父母(ConstraintLayout
)的左边缘20dp
保留。所以在这种情况下没有空白。
第二种情况与第一种情况不同。着眼于leftView
:
的leftView
总是在rightView
的左侧,因为你在leftView
和的leftView
也取决于它的父(ConstraintLayout),因为你使用的波纹管代码中使用app:layout_constraintEnd_toStartOf="@+id/rightView"
在leftView
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
和您在使用leftView
是android:layout_marginEnd="20dp"
的意思是leftView
将保留在右边20dp
与rightView
。所以在这种情况下有空白。
你可以尝试使用app:layout_constraintEnd_toEndOf="parent"
为leftView
,你会看到一个不同的。
为了得到你要的结果,你需要建立一个chain
。如果链的组件之间的所有链接都已定义,则链正确定义。
在您的代码中,rightView
必须连接到leftView
。以下约束添加到rightView
app:layout_constraintLeft_toRightOf="@+id/leftView"
,并更改leftView现有layout_constraintEnd_toStartOf
约束
app:layout_constraintRight_toLefttOf="@+id/rightView"
好奇:layout_constraintEnd_toStartOf = “@ + ID/rightView”'来:如果你还动'的应用程序,会发生什么'rightView',通过'app:layout_constraintStart_toEndOf =“@ + id/leftView”'? – stkent
@stkent几乎和第二种情况一样。但在这种情况下,除非'LayoutWidth'被设置为某个值,例如'100dp',否则'LeftView'将填充整个屏幕宽度。我也不明白这种行为。 –