2016-12-31 25 views
4

我試圖將具有4個相同大小的按鈕的水平LinearLayout轉換爲ConstraintLayout。 問題是,當我在LinearLayout中設置一個或多個按鈕到android:visibility="gone"時,其餘按鈕的大小將調整爲佔用整個空間(全部大小相同),並在ConstraintLayout中刪除按鈕,但仍佔用空間。將LinearLayout轉換爲ConstraintLayout問題

編輯:根據應用程序狀態,不同的按鈕將是可見的。

我需要更改什麼,以便ConstraintLayout的行爲與LinearLayout相似?

編輯:我發現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的內部設計工具來幫助您。您可以切換到設計選項卡並打開組件樹窗口,右鍵單擊要轉換的元素並選擇轉換爲ConstraintLayout

+0

當B2消失時,這看起來不錯,但是如果我隱藏了一個不同的按鈕,它就不能正常工作。我認爲它不是很清楚(我也會更新這個問題),但是通過編程我可以根據應用狀態改變按鈕的可見性。 – Hanan

+1

試圖使用AndroidStudio轉換工具沒有解決問題。 – Hanan

1

通過轉到佈局編輯器中的設計選項卡,可以將任何佈局轉換爲ConstraintLayout

+1

我知道,但問題是,當您將按鈕的可見性更改爲「GONE」時,它無法正確運行。 – Hanan