【发布时间】:2019-10-08 06:51:44
【问题描述】:
我正在尝试创建一个 TextView 以便它包裹其内容直到达到其最大宽度。在RecyclerView 中使用之前,以下代码可以完美地实现这一点。在我更改onBindViewHolder() 中的文本后,TextView 似乎不再测量其宽度。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/message"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_max="wrap"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.72" />
</androidx.constraintlayout.widget.ConstraintLayout>
但是请注意,如果我在下面的onBindViewHolder() 中的TextView 上调用requestLayout(),它是如何工作的:
更新:onBindViewHolder() 中的以下代码也有效。
ConstraintSet().apply {
clone(holder.parent)
applyTo(holder.parent)
}
父母是ConstraintLayout。
有人了解这里发生了什么吗?这是ConstraintLayout 中的错误吗?在onBindViewHolder() 中调用requestLayout() 是一种不好的做法,我不想这样做。
【问题讨论】:
-
remove ``` app:layout_constraintWidth_max="wrap" ``` 它将采用前面提到的宽度,即 ``` app:layout_constraintWidth_percent="0.72" ```
-
我希望它先换行,直到达到最大宽度。
-
把你的textview放到FrameLayout中。并将 app:layout_constraintWidth_max="wrap" 更改为 app:layout_constraintWidth_default="wrap" 如果在没有框架布局的情况下使用“app:layout_constraintWidth_default”,则没有 ellipsize
标签: android android-layout android-constraintlayout