在 TextView 中填充 constraintWidth_max

Padding inside TextView with constraintWidth_max

我在约束布局内有一个 TextView,用于 RecyclerView 内的项目布局。一些奇怪的行为正在发生。当应用程序第一次启动时,宽度是正确的,但是当我向上和向下滚动时,TextView 的宽度已经改变,现在正在填充额外的 space.

代码

<TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="80dp"
        android:layout_marginEnd="64dp"
        android:background="#C3C3C3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintWidth_max="wrap"
        tools:text="hello there" />

原文:

滚动后:

是否有此行为的原因,如何将其修复为预期的?

可能 android:layout_width="0dp" 导致问题或一些额外利润。

试试这个,我已经删除了额外保证金..

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#C3C3C3"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="hello there" />

我通过用 LinearLayout 替换 TextView 的包装解决了这个问题。不完全确定为什么它第一次不起作用。

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="80dp"
        android:layout_marginEnd="64dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:id="@+id/tvMessageBody"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#C3C3C3"
            tools:text="hello there" />
    </LinearLayout>