【问题标题】:How to force TextView to use as much space as possible on the first line如何强制 TextView 在第一行使用尽可能多的空间
【发布时间】:2018-11-13 23:38:26
【问题描述】:

我在创建聊天布局时遇到问题。

如何强制 TextView 的文本填满整个区域? 即使字符串是连续的,有时文本也会转到下一行。

带有 TextView 的 XML:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginBottom="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/my_msg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/message_my_shape"
    android:paddingLeft="16dp"
    android:paddingTop="8dp"
    android:paddingRight="16dp"
    android:paddingBottom="8dp"
    android:text="Hey mate,how youre doing?"
    android:textColor="#fcc7d3"
    android:textSize="16sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

更新: 伙计们,你们不太明白我的意思。填充应该在那里,我不想要一行,请查看显示布局边界的新屏幕截图,看看我在说什么。 问题还是一样,为什么 TextView 中的文本没有填满所有可用空间(有时)?

【问题讨论】:

  • 删除您在 TextView 上的填充。
  • 感谢您的回答。不幸的是,这不起作用。请查看更新后的帖子
  • 我还是不知道你在问什么。
  • 刚刚回答了我自己的问题。塔

标签: android xml textview android-constraintlayout


【解决方案1】:

找到了解决办法。

对于 API 23 及更高版本,添加到您的 TextView:

android:breakStrategy="simple"

对于 API &nbsp;(android 会自动添加它们)。

【讨论】:

    【解决方案2】:

    发生这种情况的原因是您没有指定您的TextView 只有一行。

    来自文档:

    android:maxLines

    使 TextView 最多有这么多行。当用于 可编辑文本,inputType 属性的值必须与 要应用的 maxLines 属性的 textMultiLine 标志。

    因此,如果您想始终使用单行文本,请使用:

    android:maxLines=1
    

    您的代码中的任何内容:

    <TextView
        android:id="@+id/my_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/message_my_shape"
        android:paddingLeft="16dp"
        android:paddingTop="8dp"
        android:paddingRight="16dp"
        android:paddingBottom="8dp"
        android:text="Hey mate,how youre doing?"
        android:textColor="#fcc7d3"
        android:textSize="16sp"
        android:maxLines=1
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="0dp" />
    

    【讨论】:

    • 感谢您的回答。不幸的是,事实并非如此,它需要是多行的。请查看更新后的帖子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2018-04-25
    相关资源
    最近更新 更多