当第一个视图受 layout_constraintDimensionRatio 约束时,ConstraintLayout 不再用第二个视图填充间隙
ConstraintLayout no longer fills gap with second view when first view is constrained with layout_constraintDimensionRatio
我 运行 在设计带有 ConstraintLayout
的片段时遇到了问题。我希望片段内的两个视图相互堆叠,顶部视图的比例限制为 4:3,底部视图填充其余可用的 space。所以我创建了以下布局,期待期望的行为:
<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>
然而,这种布局并没有产生预期的效果,而是在视图之间以及整个布局的顶部和底部留下了间隙:
为什么将比率应用于两个视图?有没有一种方法可以规避这种行为并仅将顶视图限制为 4:3 比例?
在写这个问题时,我实际上找到了我正在寻找的解决方案。由于 view_top
的高度已经通过比率给出,因此不需要 app:layout_constraintBottom_toTopOf="@id/view_bottom"
约束。通过简单地删除它,达到了所需的行为,视图看起来像这样,没有任何间隙:
我 运行 在设计带有 ConstraintLayout
的片段时遇到了问题。我希望片段内的两个视图相互堆叠,顶部视图的比例限制为 4:3,底部视图填充其余可用的 space。所以我创建了以下布局,期待期望的行为:
<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>
然而,这种布局并没有产生预期的效果,而是在视图之间以及整个布局的顶部和底部留下了间隙:
为什么将比率应用于两个视图?有没有一种方法可以规避这种行为并仅将顶视图限制为 4:3 比例?
在写这个问题时,我实际上找到了我正在寻找的解决方案。由于 view_top
的高度已经通过比率给出,因此不需要 app:layout_constraintBottom_toTopOf="@id/view_bottom"
约束。通过简单地删除它,达到了所需的行为,视图看起来像这样,没有任何间隙: