2017-10-20 122 views
0

無論屏幕大小如何,我都需要建立一個始終在整個屏幕寬度(縱向)上分佈的正方形。在此平方和一些固定高度小部件下方,我想將剩餘的可用空間分配給文本字段。如何強制小部件的水平約束優先於其垂直約束?

下面的簡化的代碼顯示了我迄今嘗試:

<?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" 
tools:context=".ScMainFull" 
> 

<android.support.constraint.ConstraintLayout 
    android:id="@+id/Grid" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    app:layout_constraintDimensionRatio="1:1" 
    android:layout_margin="16dp" 
    app:layout_constraintVertical_chainStyle="spread_inside" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintBottom_toTopOf="@+id/button1" 
    /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="0dp" 
    android:layout_height="48dp" 
    android:text="1" 
    android:layout_margin="16dp" 
    app:layout_constraintTop_toBottomOf="@+id/Grid" 
    app:layout_constraintBottom_toTopOf="@+id/Report" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    /> 

<TextView 
    android:id="@+id/Report" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_margin="16dp" 
    app:layout_constraintTop_toBottomOf="@+id/button1" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    /> 

</android.support.constraint.ConstraintLayout> 

在垂直軸上,可用的空間(總高度較小的垂直邊緣和固定的高度按鈕)的兩個小部件之間均勻地分佈該有layout_height="0dp"忽略第一個小部件中的水平約束。

在這個代碼中,我應該更改哪些空間,以便在佔位整個屏幕寬度後構建正方形後,將空間分配給textview?提出同樣問題的另一種方法是:如何強制平方上的水平約束優先於垂直約束?

+1

你不得不使用ConstraintLayout嗎?否則,你可以使用垂直LinearLayout來實現你想要的。 – Michele

+0

是的,父母必須是ConstraintLayout。這使我可以使用'layout_constraintDimensionRatio =「1:1」'來輕鬆獲得一個正方形。上面的簡化代碼中沒有顯示的其他視圖也要求父級是ConstraintLayout。 正方形視圖可以是任何類型的小部件。我曾嘗試過使用「視圖」,但問題是一樣的。 – ema3272

回答

0

只需從連鎖店中刪除平方頂部ConstraintLayout即可。

<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.support.constraint.ConstraintLayout 
     android:id="@+id/Grid" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_constraintDimensionRatio="1" 
     android:layout_margin="16dp" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="48dp" 
     android:text="1" 
     android:layout_margin="16dp" 
     app:layout_constraintVertical_chainStyle="spread_inside" 
     app:layout_constraintTop_toBottomOf="@+id/Grid" 
     app:layout_constraintBottom_toTopOf="@+id/Report" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     /> 

    <TextView 
     android:id="@+id/Report" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_margin="16dp" 
     app:layout_constraintTop_toBottomOf="@+id/button1" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     /> 

</android.support.constraint.ConstraintLayout> 

如果您正在考慮嵌套另一個ViewGroup,那麼您就錯了。