2016-12-31 9 views
4

같은 크기의 4 개의 버튼이있는 LinearLayout의 가로를 ConstraintLayout으로 변환하려고합니다. 문제는 하나 이상의 버튼을 LinearLayout에서 android:visibility="gone"으로 설정하면 나머지 버튼이 전체 공간을 차지하도록 크기가 조정되고 (모두 동일한 크기 임) ConstraintLayout 버튼은 제거되지만 여전히 공간을 차지합니다.LinearLayout을 ConstraintLayout 문제로 변환

편집 : 앱 상태에 따르면 다른 버튼이 표시됩니다.

ConstraintLayoutLinearLayout처럼 동작하도록 변경하려면 무엇이 필요합니까?

편집 : ConstraintLayout (제약 조건 참조)에서 실수를 발견하여 이미지와 이미지 (문제는 여전히 존재 함)를 발견했습니다.

LinearLayout XML :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/b1" 
     android:text="B1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     /> 

    <Button 
     android:id="@+id/b2" 
     android:text="B2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     android:visibility="gone" 
     /> 

    <Button 
     android:id="@+id/b3" 
     android:text="B3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     /> 

    <Button 
     android:id="@+id/b4" 
     android:text="B4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     /> 
</LinearLayout> 

편집 :ConstraintLayout XML :

<?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"> 

    <Button 
     android:id="@+id/b1" 
     android:text="B1" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/b2" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintHorizontal_weight="1" 
     /> 

    <Button 
     android:id="@+id/b2" 
     android:text="B2" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:visibility="gone" 
     app:layout_constraintLeft_toRightOf="@+id/b1" 
     app:layout_constraintRight_toLeftOf="@+id/b3" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintHorizontal_weight="1" 
     /> 

    <Button 
     android:id="@+id/b3" 
     android:text="B3" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toRightOf="@+id/b2" 
     app:layout_constraintRight_toLeftOf="@+id/b4" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintHorizontal_weight="1" 
     android:layout_marginTop="0dp"/> 

    <Button 
     android:id="@+id/b4" 
     android:text="B4" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toRightOf="@+id/b3" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintHorizontal_weight="1" 
     /> 
</android.support.constraint.ConstraintLayout> 

LinearLayout vs ConstraintLayout ConstraintLayout blueprint

+0

왜 제대로 작동하는지 선형 레이아웃을 바꾸시겠습니까? 천천히? 그것은 많은 기억을 먹는가? – pskink

+0

복잡한 레이아웃 구조가 있으므로 제약 레이아웃으로 변환 중입니다. 이것은 내가 변환 할 수 없었던 한 부분입니다. – Hanan

+0

선형 레이아웃을 구속 레이아웃에 포함시킬 수는 있지만이를 피하고 싶습니다 (구속 조건 레이아웃의 목적은 깊은 레이아웃 계층을 제거하는 것입니다). – Hanan

답변

1

당신은 아마 같은 당신의 레이아웃을 변경할 수 있습니다

<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/b1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="B1" 
     app:layout_constraintBaseline_toBaselineOf="@+id/b3" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/b3" 
     tools:layout_constraintBaseline_creator="1" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" /> 

    <Button 
     android:id="@+id/b2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="B2" 
     android:visibility="gone" 
     app:layout_constraintBottom_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     tools:layout_constraintBottom_creator="1" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintTop_creator="1" /> 

    <Button 
     android:id="@+id/b3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="128dp" 
     android:layout_marginLeft="128dp" 
     android:layout_marginRight="128dp" 
     android:layout_marginStart="128dp" 
     android:text="B3" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintTop_creator="1" /> 

    <Button 
     android:id="@+id/b4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="B4" 
     app:layout_constraintBaseline_toBaselineOf="@+id/b3" 
     app:layout_constraintLeft_toRightOf="@+id/b3" 
     app:layout_constraintRight_toRightOf="parent" 
     tools:layout_constraintBaseline_creator="1" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" /> 

</android.support.constraint.ConstraintLayout> 

기존 레이아웃을 ConstraintLayout으로 전환하는 데 어려움이있는 경우 Android Studio의 내부 디자인 도구를 사용해보세요. Design 탭으로 전환하여 구성 요소 트리 창을 열고 변환 할 요소를 마우스 오른쪽 버튼으로 클릭하고 Convert to ConstraintLayout을 선택하십시오.

+0

B2가 사라지면 괜찮아 보이지만 다른 버튼을 숨기면 제대로 작동하지 않습니다. 나는 그것이 명확하지 않다 (나는 또한 질문을 업데이트 할 것이다)라고 가정하지만, 프로그래밍 방식으로 나는 앱 상태에 따라 버튼의 가시성을 바꾼다. – Hanan

+1

AndroidStudio 변환 도구를 사용해도 문제가 해결되지 않았습니다. – Hanan

1

레이아웃 편집기의 디자인 탭으로 이동하여 모든 레이아웃을 ConstraintLayout으로 변환 할 수 있습니다.

+1

알아요,하지만 문제는 버튼의 가시성을 "사라짐"으로 바꿀 때 올바르게 동작하지 않는다는 것입니다. – Hanan