2017-10-20 96 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,那么您就错了。