2017-09-27 2 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

사람은이가 예상 된 동작 여부를 설명 할 수 ... 사라? rightView 부모 ( ConstraintLayout)에 의존하기 때문에

+0

호기심에 leftView의 기존 layout_constraintEnd_toStartOf 제약 조건을 변경 :'layout_constraintEnd_toStartOf = "@ + ID/rightView"당신이 추가로'이동 응용하면 어떻게됩니까 'rightView'를 통해 'app : layout_constraintStart_toEndOf = "@ + id/leftView"'? – stkent

+0

@stkent 두 번째 경우와 거의 같은 동작입니다. 그러나이 경우'LayoutView '는'LayoutWidth'가'100dp'와 같은 어떤 값으로 설정되어 있지 않으면 화면의 전체 너비를 채 웁니다. 나는 그 행동을 이해하지 못한다. –

답변

2

leftviewrightview에 달려 있으므로 leftview의 여백을 rightview으로 설정할 수 있습니다.

보기에 여백이있는 경우보기가 의보기에 따라에 공백이 있음을 의미합니다. rightviewleftview에을 DEPENDING 하지 않기 때문에

당신이 rightview에서 android:layout_marginStart="20dp"를 작성하는 경우, 그것은 leftview에 공간을 제공하지 않습니다.

+1

감사합니다. 이것은 아마도 문제에 대한 가장 명확하고 가장 간결한 설명 일 것입니다. – Gus

0

, 당신은 설정하십시오 parentConstraintLayout입니다

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

합니다. 당신은 당신이 rightView

그래서 당신이 rightViewandroid:layout_marginStart="20dp"을 사용하는 부모 ( ConstraintLayout)의 오른쪽 상단에 있던 만든 것으로했다. rightView은 부모가 ( ConstraintLayout) 인 왼쪽 20dp의 여백을 남깁니다. 이 경우 공백이 없습니다.


두 번째 경우는 첫 번째 경우와 다릅니다. leftView에 보이는 :

을 당신이 leftView

또한 당신이 울부 짖는 코드를 사용하기 때문에 부모 (ConstraintLayout)에 따라 달라집니다 leftViewapp:layout_constraintEnd_toStartOf="@+id/rightView"을 사용하기 때문에 leftViewrightView의 왼쪽에 항상 leftView

app:layout_constraintStart_toStartOf="parent" 
app:layout_constraintTop_toTopOf="parent" 

과는 당신은 leftViewandroid:layout_marginEnd="20dp"를 사용하는 의 의미는 leftView이고 오른쪽은 20dp이고, rightView입니다. 이 경우에는 공백이 있습니다.


당신은 leftView 다른를 볼 수에 대한 app:layout_constraintEnd_toEndOf="parent"를 사용하려고 할 수 있습니다.

0

원하는 결과를 얻으려면 chain을 만들어야합니다. 체인의 구성 요소 사이에 모든 링크가 정의되면 체인이 올바르게 정의됩니다.

코드에서 rightViewleftView에 연결되어야합니다. rightView

app:layout_constraintLeft_toRightOf="@+id/leftView" 

에 다음과 같은 제약 조건을 추가하고

app:layout_constraintRight_toLefttOf="@+id/rightView" 
관련 문제