ConstraintLayout beta5中的wrap_content不能正确地包裹内容

4

我有一个用于RecyclerView中ViewHolder的XML布局。

这个布局的根元素是一个高度设置为wrap_content的ConstraintLayout。

在这个扁平的层级结构中,有三个文本视图和一个固定高度的图像视图;可以将其想象为:

<ConstraintLayout>
     <TextView height=wrap>
     <TextView height=wrap>
     <TextView height=wrap>
     <ImageView height=150dp>
</ConstraintLayout>

这是一个相对简单的布局。在beta4中,它在设计师中的显示方式如下(最终在运行时,每个recyclerView单元格都是这样的):

Beta4

抱歉因为保密协议等原因需要遵守一些程序。
话说回来,这里有以下元素:
三个文本视图(带紫色背景的红色贴带)和高度为150dp的ImageView(灰色部分)。
紫色背景应用于根ConstraintLayout。非常好。
现在看起来是这样的,在Beta 5中没有进行任何修改:

beta5

如您所见,紫色(根)约束布局现在“混乱”了,不再像以前一样包裹内容。

我尝试过的事情:

  1. app:layout_constraintHeight_default="wrap" 添加到 ConstraintLayout 中(也进行了扩展)。没有任何改变。

  2. ImageView 具有一个 app:layout_constraintBottom_toBottomOf="parent" 约束,我尝试删除它,但也没有任何改变。

  3. 回滚到 beta4 :)

为记录,这是完整的布局(由于某些原因,id 已被重命名,并且没有 tools:text 或类似内容)。布局在其他方面完全相同。

<?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="wrap_content"
    android:background="@color/colorAccent">

    <TextView
        android:id="@+id/toplabel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text=""
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/top_bottom_label"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/top_right_label"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/top_right_label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:ellipsize="end"
        android:gravity="end"
        android:maxLines="1"
        android:text=""
        app:layout_constraintBottom_toTopOf="@+id/top_bottom_label"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toRightOf="@+id/toplabel"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/top_bottom_label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:ellipsize="end"
        android:gravity="end"
        android:maxLines="1"
        android:text=""
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toRightOf="@+id/toplabel"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_right_label" />

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_bottom_label"
        app:srcCompat="@android:color/darker_gray" />

</android.support.constraint.ConstraintLayout>

我需要做些不同的事情吗?(我知道可以用RelativeLayout替换并可能实现相同的效果,但是无论如何...我相信ConstraintLayout!):)

1个回答

6

报告了一个bug,并得到了解决方法。

这是一个回归问题,会被修复(我们希望如此),但是...结果发现我的链条也定义不正确。我的top_bottom_label 没有底部端点,根据文档,链中的元素应该在两个端点上连接

因此,我添加了app:layout_constraintBottom_toTopOf="@id/imageview"top_bottom_label,这似乎对我的情况有效。实际上,我已经将imageView添加到了链中,尽管我并不是很在意它。现在可以使用了。

更新于2017年2月14日:ConstraintLayout团队@ Google已经在master版中修复了这个问题。它很可能在下一个版本中得到修复。(感谢!)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接