2017-07-13 81 views
6

我使用ConstraintLayout在那裏我將顯示如下ConstraintLayout,當約束依賴視消失,佈局視圖古怪行爲

enter image description here

我想隱藏First(採用了),並視圖我希望如下(這裏可以將ElasticBody上伸展使用了原來First視圖空間爲好。

enter image description here

然而,磨片ñ我實際設置Firstgone,我的看法如下(所有圖像來自Android Studio Design視圖)。我的Elastic Body也不見了,高度奇怪地擴大了。

enter image description here

我的佈局代碼如下

<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="wrap_content" 
    android:padding="16dp"> 

    <TextView 
     android:id="@+id/txt_first" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#0ff" 
     android:text="First" 
     android:visibility="gone" 
     android:textSize="26sp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintEnd_toStartOf="@+id/txt_body" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <TextView 
     android:id="@+id/txt_body" 
     android:layout_width="0dp" 
     android:background="#f0f" 
     android:layout_height="wrap_content" 
     android:text="Elastic Body" 
     android:textSize="26sp" 

     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintEnd_toStartOf="@+id/txt_tail" 
     app:layout_constraintStart_toEndOf="@+id/txt_first" 
     app:layout_constraintTop_toBTopOf="parent" /> 

    <TextView 
     android:id="@+id/txt_tail" 
     android:background="#ff0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Tail" 
     android:textSize="26sp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintStart_toEndOf="@+id/txt_body" 
     app:layout_constraintTop_toTopOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

(請注意,如果您刪除gone,你會得到的第一個圖像視圖)。 這是爲什麼呢?我該如何修復它,當我的First不見了,我可以將Elastic Body伸展出來嗎?

p/s:我知道如何在LinearLayout和RelativeLayout中做到這一點......但不知道這是否是對ConstraintLayout的限制?

回答

1

See Output :

嘗試以下。您可以在ConstraintLayout使用Visibility.GONE

<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="wrap_content" 
    android:padding="16dp"> 

    <TextView 
     android:id="@+id/txt_first" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#0ff" 
     android:text="First" 
     android:textSize="26sp" 
     android:visibility="gone" 
     app:layout_constraintEnd_toStartOf="@+id/txt_body" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" /> 

    <TextView 
     android:id="@+id/txt_body" 
     android:layout_width="0dp" 
     android:background="#f0f" 
     android:layout_height="wrap_content" 
     android:text="Elastic Body" 
     android:textSize="26sp" 
     app:layout_constraintRight_toLeftOf="@+id/txt_tail" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toRightOf="@+id/txt_first" 
     /> 

    <TextView 
     android:id="@+id/txt_tail" 
     android:background="#ff0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Tail" 
     android:textSize="26sp" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintRight_toRightOf="parent" /> 

</android.support.constraint.ConstraintLayout> 
+0

感謝。有趣的是,從'tail'移除'app:layout_constraintStart_toEndOf =「@ + id/txt_body」'有助於。 Upvote你的答案。你能否闡述背後的邏輯?謝謝! (不知道誰不投票,他們至少應該給出一個理由......) – Elye

+0

想了一會兒,我想我明白了邏輯。我將你的答案作爲答案。謝謝! (如果你認爲有助於他人,請加我的問題) – Elye

+0

請給我投票的理由。 –

2

一兩件事。你可以使用Barriers

,如果你不知道有關Barriers那麼請檢查:Barriers

相關問題