在ConstraintLayout中使用时,我无法理解边距(android:layout_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>
...产生以下输出......
但是,当我将保证金从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>
......保证金意外消失......
有人可以解释这是否是预期的行为,如果是,为什么会这样?
答案 0 :(得分:3)
由于leftview
取决于rightview
,因此您可以将leftview
的边距设置为rightview
。
当视图有边距时,表示视图提供空间视图依赖。
如果您在android:layout_marginStart="20dp"
中写rightview
,则不会为leftview
留出空间,因为rightview
不是依赖到leftview
}。
答案 1 :(得分:0)
由于rightView
取决于其父级(ConstraintLayout
),因此您需要设置:
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
parent
是ConstraintLayout
。您这样做是因为rightView
位于其父级的右上角(ConstraintLayout
)
所以你在android:layout_marginStart="20dp"
中使用了rightView
。 rightView
将保留20dp
与其父(ConstraintLayout
)的边距。所以在这种情况下没有空格。
第二种情况与第一种情况不同。查看leftView
:
leftView
始终位于rightView
的左侧,因为您在app:layout_constraintEnd_toStartOf="@+id/rightView"
leftView
AND leftView
还取决于其父级(ConstraintLayout),因为您在leftView
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
AND 您在android:layout_marginEnd="20dp"
中使用了leftView
,这意味着leftView
将20dp
与rightView
对齐app:layout_constraintEnd_toEndOf="parent"
。所以在这种情况下有空白区域。
您可以尝试leftView
使用*.csv
,您会看到不同的内容。
答案 2 :(得分:0)
要获得您要查找的结果,您需要构建chain
。如果链的组件之间的所有链接都已定义,则链被正确定义。
在您的代码中,rightView
必须与leftView
相关联。将以下约束添加到rightView
app:layout_constraintLeft_toRightOf="@+id/leftView"
并将leftView中现有的layout_constraintEnd_toStartOf
约束更改为
app:layout_constraintRight_toLefttOf="@+id/rightView"