【问题标题】:Simple constraint layout child with max width in RecyclerViewRecyclerView 中具有最大宽度的简单约束布局子项
【发布时间】: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


【解决方案1】:

我遇到了同样的问题,我通过使用RelativeLayout 并在TextView 中设置属性android:maxWidth 来解决它。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content">

       <TextView
            android:id="@+id/textViewChat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autoLink="all"
            android:gravity="start"
            android:linksClickable="true"
            android:maxWidth="256dp"
            android:padding="8dp"
            android:textColor="@color/black"
            android:textSize="14sp" />
</RelativeLayout>

【讨论】:

    【解决方案2】:

    我认为您的视图在回收过程中没有被重绘。检查你是否有 为 recyclerview 设置“setHasFixedSize=true”。删除它或使其为假。如果它仍然不起作用,请尝试将 TextView 宽度更改为 wrap_content 而不是 0dp。

    【讨论】:

    • setHasFixedSize 没有帮助。 wrap_content 使其在接触过多时不尊重 app:layout_constraintWidth_percent,即它扩展为占据整个屏幕的宽度。
    【解决方案3】:

    我知道它晚了一个月,但我最近遇到了这个问题,也找不到答案。作者提出的不断变化的 ConstraintSet() 有效,但始终牢记这个问题是否有效。另一种解决方案是将app:layout_constraintWidth_max="wrap" 更改为app:layout_constraintWidth_default="wrap" 这对我有用,recyclerView 在回收视图持有者时正确更新约束

    【讨论】:

      猜你喜欢
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 2020-11-15
      • 1970-01-01
      • 2017-11-07
      • 1970-01-01
      相关资源
      最近更新 更多