【问题标题】:TextView with drawableEnd in ConstraintLayout line wrap issue在 ConstraintLayout 换行问题中带有 drawableEnd 的 TextView
【发布时间】:2021-11-17 19:40:12
【问题描述】:

我有一个ConstraintLayout,其中包含一个TextView,在文本末尾有一个小图像。我遇到的这个问题是长文本没有换行。如果我修复了TextView 上的约束,以便长文本将图像末尾换行到屏幕末尾:

截图:

如果我将app:layout_constraintEnd_toEndOf="parent" 添加到TextView,则长文本会换行,但最后的图像不会:

代码:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    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"
    android:padding="10dp"
    android:layout_marginBottom="20dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/subscription_row_item_thumb"
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:scaleType="centerInside"
        android:background="@drawable/all_circle_white_bg"
        android:padding="1dp"
        android:foreground="?android:attr/selectableItemBackground"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <ImageView
        android:id="@+id/subscription_row_item_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_marginTop="15dp"
        app:layout_constraintTop_toBottomOf="@id/subscription_row_item_thumb"
        app:layout_constraintStart_toStartOf="@id/subscription_row_item_thumb"
        app:layout_constraintEnd_toEndOf="@id/subscription_row_item_thumb"/>

     <TextView
        android:id="@+id/subscription_row_item_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:breakStrategy="simple"
        android:layout_marginStart="15dp"
        android:textSize="16sp"
        app:layout_constrainedWidth="true"
        app:drawableEndCompat="@drawable/ic_action_open_episode_list"
        android:drawablePadding="10dp"
        app:layout_constraintTop_toTopOf="@id/subscription_row_item_thumb"
        app:layout_constraintBottom_toBottomOf="@id/subscription_row_item_thumb"
        app:layout_constraintStart_toEndOf="@id/subscription_row_item_thumb"
        app:layout_constraintEnd_toEndOf="parent"/>

    <TextView
        android:id="@+id/subscription_row_item_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:breakStrategy="simple"
        android:layout_marginTop="5dp"
        android:textSize="14sp"
        android:textColor="#A1A0A0"
        app:layout_constraintTop_toBottomOf="@id/subscription_row_item_title"
        app:layout_constraintStart_toStartOf="@id/subscription_row_item_title"/>

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 如果文本足够长,您是否希望您的图标不超出父屏幕,并用省略号截断溢出的文本??
  • 不清楚你想要达到什么目的。
  • 在第二行,我想用小图标包裹文字
  • 用文本换行是什么意思?
  • 我希望小图标始终位于文本的末尾,即使在换行时也是如此。

标签: android android-layout textview android-constraintlayout


【解决方案1】:

要解决此问题,您需要:

  1. wrap_content 保持drawableEnd 粘贴到文本末尾的宽度,并避免由于app:layout_constraintEnd_toEndOf="parent" 而将其粘贴到末尾
  2. 设置app:layout_constraintHorizontal_bias="0" 以便将TextView 偏向开头

TextView内容展开时,app:layout_constraintEnd_toEndOf="parent"会生效

将这些应用到TextView

 <TextView
    android:id="@+id/subscription_row_item_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:breakStrategy="simple"
    android:layout_marginStart="15dp"
    android:textSize="16sp"
    app:layout_constrainedWidth="true"
    app:layout_constraintHorizontal_bias="0"
    app:drawableEndCompat="@drawable/ic_action_open_episode_list"
    android:drawablePadding="10dp"
    app:layout_constraintTop_toTopOf="@id/subscription_row_item_thumb"
    app:layout_constraintBottom_toBottomOf="@id/subscription_row_item_thumb"
    app:layout_constraintStart_toEndOf="@id/subscription_row_item_thumb"
    app:layout_constraintEnd_toEndOf="parent"/>

【讨论】:

  • 但我希望图标与文本一起换行。所以它总是在最后一个单词的右边。因此,在您的顶部屏幕截图中,邮件图标将位于第二行“文本”一词的右侧。我认为这是不可能的,或者我认为这是错误的。
  • 这不是drawableEnd的行为。顾名思义,它是 textView 框末尾的可绘制对象。不是文本本身。您需要在文本中添加可绘制对象作为内容。这与 ConstraintLayout 无关
  • @KrisB This 可以帮助你
  • 我明白了。我想我很困惑。你发送的那个链接有帮助。我需要将图标添加到文本本身。谢谢!
【解决方案2】:

TextView 关联的可绘制对象的位置适用于 TextView 本身的开始、顶部、结束和底部,而不是其中包含的文本。这是您看到的行为。

您可以使用ImageSpan 将可绘制对象与文本末尾关联,但这必须以编程方式完成。

    val image = ContextCompat.getDrawable(this, R.drawable.ic_baseline_open_in_new_24)!!
    val imageSize = textView.lineHeight / 2
    image.updateBounds(bottom = imageSize, right = imageSize)
    val imageSpan = ImageSpan(
        image,
        DynamicDrawableSpan.ALIGN_BASELINE
    )
    val spannable = SpannableString("${textView.text} ")
    spannable.setSpan(
        imageSpan, spannable.length - 1, spannable.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE
    )
    textView.text = spannable

这里的图标位于文本的末尾并替换最后的空格字符。

【讨论】:

  • 是的,这就是我要找的。谢谢!
【解决方案3】:

也许会有所帮助

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    android:padding="10dp"
    android:layout_marginBottom="20dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/subscription_row_item_thumb"
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:scaleType="centerInside"
        android:background="@drawable/all_circle_white_bg"
        android:padding="1dp"
        android:foreground="?android:attr/selectableItemBackground"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <ImageView
        android:id="@+id/subscription_row_item_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_marginTop="15dp"
        app:layout_constraintTop_toBottomOf="@id/subscription_row_item_thumb"
        app:layout_constraintStart_toStartOf="@id/subscription_row_item_thumb"
        app:layout_constraintEnd_toEndOf="@id/subscription_row_item_thumb"
        />

    <LinearLayout
        android:id="@+id/layout_item_title"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:drawablePadding="10dp"
        app:layout_constraintTop_toTopOf="@id/subscription_row_item_thumb"
        app:layout_constraintBottom_toBottomOf="@id/subscription_row_item_thumb"
        app:layout_constraintStart_toEndOf="@id/subscription_row_item_thumb"
        app:layout_constraintEnd_toEndOf="parent">

        <TextView
            android:id="@+id/subscription_row_item_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:breakStrategy="simple"
            android:layout_marginStart="15dp"
            android:textSize="16sp"
            app:layout_constrainedWidth="true"
            app:drawableEndCompat="@drawable/ic_action_open_episode_list"/>

    </LinearLayout>

    <TextView
        android:id="@+id/subscription_row_item_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:breakStrategy="simple"
        android:layout_marginTop="5dp"
        android:layout_marginStart="15dp"
        android:textSize="14sp"
        android:textColor="#A1A0A0"
        app:layout_constraintTop_toBottomOf="@id/layout_item_title"
        app:layout_constraintStart_toStartOf="@id/layout_item_title"/>

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 是的,我想到了那个 put 我希望有一个更优雅的解决方案。我的意思是它不是那么长的文字。
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
  • 2017-01-23
  • 2021-06-26
相关资源
最近更新 更多