当第一个视图受layout_constraintDimensionRatio约束时,ConstraintLayout不再填补第二个视图的空白

问题描述 投票:0回答:1

设计带有ConstraintLayout的片段时遇到问题。我希望片段内的两个视图相互堆叠,顶视图的比例限制为4:3,而底视图则填充其余可用空间。因此,我创建了以下布局,并期望了所需的行为:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/view_top"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/view_bottom"
        app:layout_constraintDimensionRatio="H,4:3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/view_bottom"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/view_top" />

</ConstraintLayout>

但是,此布局不会产生所需的效果,而是在视图之间以及整个布局的顶部和底部之间留有间隙:

Undesired gaps in the layout

为什么将比例应用于两个视图?有没有办法避免这种行为,只将顶视图限制为4:3的比例?]

android android-layout android-constraintlayout
1个回答
0
投票

在写这个问题时,我实际上找到了我想要的解决方案。由于view_top的高度已经通过比例给出,因此不需要app:layout_constraintBottom_toTopOf="@id/view_bottom"约束。通过简单地删除它,可以达到所需的行为,并且视图看起来像这样,没有任何间隙:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.