【问题标题】:ConstraintLayout no longer fills gap with second view when first view is constrained with layout_constraintDimensionRatio当第一个视图受 layout_constraintDimensionRatio 约束时,ConstraintLayout 不再填充与第二个视图的间隙
【发布时间】:2020-05-29 11:21:24
【问题描述】:

我在设计带有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>

但是,这种布局不会产生预期的效果,而是会在视图之间以及整个布局的顶部和底部留下空隙:

为什么比率会同时应用于两个视图?有没有办法规避这种行为,并且只将顶视图限制为 4:3 比例?

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      相关资源
      最近更新 更多