首页 > 解决方案 > 每行带有 layout_constraintHeight_min 的 Flat ConstraintLayout 不起作用

问题描述

我有一个我认为很常见的用例:屏幕上有多行信息。我希望实现这些视图而不必使用嵌套的 ViewGroups。每行应该有一个最小高度,但如果内容大于最小高度,则展开。内容应垂直嵌套。

看起来这个简化的示例应该可以工作:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Row"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/view"
        app:layout_constraintBottom_toBottomOf="@+id/view"/>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#8800ff00"
        app:layout_constraintHeight_min="100dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/textView"/>

</android.support.constraint.ConstraintLayout>

我没有为每一行创建一个 ViewGroup,而是有一个带有约束的平面布局。每行的视图设置行高并且可以单击。行内的视图是布局层次结构中的兄弟,但它们在该行视图的中间垂直居中。由于我已经指定app:layout_constraintHeight_min并将视图的底部锚定到内容的底部,它应该随着内容的增长而增长。

但是有一个问题:

在此处输入图像描述

ConstraintLayout 在行上方添加了不需要的间距!请注意,行上方不需要的间距等于内容底部和行底部之间的正确间距。

我的理论是这样的:由于视图的底部锚定在内容的底部(TextView),它想要紧紧地粘在它旁边并紧挨着它。如果我强迫它移得更远,它会添加类似底部边距的东西来实现这一点,它会添加类似的顶部边距以保持对称。

我如何让它停止?如果不是因为顶部不需要的间距,我会拥有我需要的东西。也许有一些特殊的 ConstraintLayout 技巧,一些神奇的属性来修复这种行为。或者也许有一种完全不同的方式来使用 ConstraintLayout 来完成我想要的 UI。

我意识到使用固定高度的行会使这变得更简单,但如果内容可以增长,我不喜欢这样做。

我可以将我的 UI 更改为每行都有一个嵌套的 ConstraintLayout,但在我努力使复杂的布局完全平坦、没有多层 ViewGroups 之后,我宁愿不这样做。但是,如果我找不到更好的解决方案,我会这样做,我希望在这里找到。

标签: androidandroid-layoutandroid-constraintlayout

解决方案


我认为删除最小高度并在 TextView 中添加一些边距就可以了,如果可能的话,您可以使用约束而不是使用 match_parent

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="50dp"
        android:text="Row is not column"
        android:textColor="#283858"
        app:layout_constraintBottom_toBottomOf="@+id/view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/view"/>

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#8800ff00"
        app:layout_constraintBottom_toBottomOf="@id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的输出


推荐阅读