2017-09-27 93 views
3

在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> 

...产生以下输出...

Expected Output

然而,当我改变从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> 

...保证金意外消失......

Unexpected Output

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

+0

好奇:layout_constraintEnd_toStartOf = “@ + ID/rightView”'来:如果你还动'的应用程序,会发生什么'rightView',通过'app:layout_constraintStart_toEndOf =“@ + id/leftView”'? – stkent

+0

@stkent几乎和第二种情况一样。但在这种情况下,除非'LayoutWidth'被设置为某个值,例如'100dp',否则'LeftView'将填充整个屏幕宽度。我也不明白这种行为。 –

回答

2

因为leftview取决于rightview,因此您可以将边距leftview设置为rightview

当视图有余量时,这意味着视图给出的空间为视图取决于。如果你写在rightviewandroid:layout_marginStart="20dp"

,它不会leftview给予空间,因为rightview不是DEPENDINGleftview

+1

谢谢。这可能是对这个问题最清楚和最简洁的解释。 – Gus

0

因为rightView取决于它的父(ConstraintLayout),您可以设置:

app:layout_constraintEnd_toEndOf="parent" 
app:layout_constraintTop_toTopOf="parent" 

parentConstraintLayout。你做了,你让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" 

您在使用leftViewandroid:layout_marginEnd="20dp"的意思是leftView将保留在右边20dprightView。所以在这种情况下有空白。


你可以尝试使用app:layout_constraintEnd_toEndOf="parent"leftView,你会看到一个不同的。

0

为了得到你要的结果,你需要建立一个chain。如果链的组件之间的所有链接都已定义,则链正确定义。

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

app:layout_constraintLeft_toRightOf="@+id/leftView" 

,并更改leftView现有layout_constraintEnd_toStartOf约束

app:layout_constraintRight_toLefttOf="@+id/rightView"