Mar*_*ini 4 android android-constraintlayout constraint-layout-chains
我在RecyclerView中有一个ViewHolder的XML布局.
此布局的根是ConstraintLayout,其高度设置为wrap_content.
在这个平面层次结构中,有3个文本视图和一个具有固定高度的图像视图; 考虑到:
<ConstraintLayout>
<TextView height=wrap>
<TextView height=wrap>
<TextView height=wrap>
<ImageView height=150dp>
</ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
这是一个相对简单的布局.在beta4这是它的外观在设计(并最终在运行时,每个recyclerView细胞):
为"繁文缛节"道歉,但它是NDA等等.
话虽这么说,要素是:
3个文本视图(红色录制带有漂亮的紫色背景)高度为150dp的ImageView是灰色的.
紫色背景应用于根ConstraintLayout.一切都好.
现在这就是它的外观,不需要对Beta 5进行任何修改:
正如您所看到的,紫色(根)约束布局现在"混淆",并且不会像过去那样包装内容.
我试过的事情:
添加app:layout_constraintHeight_default="wrap"到ConstraintLayout(并传播).没有什么区别.
ImageView有一个app:layout_constraintBottom_toBottomOf="parent"我试图删除的约束,也没有任何区别.
恢复到beta4 :)
为了记录,这是完整的布局(由于相同的原因,id已被重命名为出于繁文缛节的原因而没有工具:文本或类似的).布局在其他方面完全相同.
<?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>
Run Code Online (Sandbox Code Playgroud)
我应该做些不同的事吗?(我知道我可以用RelativeLayout代替它,也可能做同样的事情,但无论如何......我相信 ConstraintLayout!):)
这是一个回归,将被修复(我们希望),但......结果我的链也被错误定义.我top_bottom_label 没有底部端点,并且根据文档链中的元素应该连接在两个端点上.
所以我加入app:layout_constraintBottom_toTopOf="@id/imageview"了top_bottom_label,这似乎适用于我的情况.我有效地将imageView添加到链中,即使我真的不太关心它.它现在有效.
2017年2月14日更新:ConstraintLayout团队@ Google在master中解决了这个问题.它可能会在下一个版本中修复.(谢谢!).