在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>
...保证金意外消失......
有人能解释这是否是预期的行为,如果是这样,这是为什么?
好奇:layout_constraintEnd_toStartOf = “@ + ID/rightView”'来:如果你还动'的应用程序,会发生什么'rightView',通过'app:layout_constraintStart_toEndOf =“@ + id/leftView”'? – stkent
@stkent几乎和第二种情况一样。但在这种情况下,除非'LayoutWidth'被设置为某个值,例如'100dp',否则'LeftView'将填充整个屏幕宽度。我也不明白这种行为。 –