【问题标题】:Padding inside TextView with constraintWidth_max使用 constraintWidth_max 在 TextView 内填充
【发布时间】:2020-07-17 19:16:01
【问题描述】:

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

代码

<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 android-layout android-recyclerview textview android-constraintlayout


【解决方案1】:

可能是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" />

【讨论】:

  • 谢谢,不过这对我来说并不适用。我确实发布了一个社区 wiki 答案,其中包含什么有用的
【解决方案2】:

我通过用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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    相关资源
    最近更新 更多