我有一个TextView,我想限制它的字符。实际上,我可以这样做,但我正在寻找的事情是如何在字符串的末尾添加三个点(…)。这一个显示文本已经继续。这是我的XML,但没有点,尽管它限制了我的文本。

<TextView 
        android:id                      = "@+id/tvFixture"
        android:layout_width            = "wrap_content"
        android:layout_height           = "wrap_content"
        android:layout_toLeftOf         = "@id/ivFixture_Guest"
        android:text                    = "@string/test_06"
        android:lines                   = "1"
        android:ems                     = "3"
        android:gravity                 = "right"
        style                           = "@style/simpletopic.black" 
        android:ellipsize="end"/>

当前回答

Use

android:单行模式= " true " android: maxLines = " 1 " 应用:layout_constrainedWidth = " true "

这是我的完整的TextView看起来:

    <TextView
    android:id="@+id/message_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="5dp"
    android:maxLines="1"
    android:singleLine="true"
    android:text="NAME PLACEHOLDER MORE Text"
    android:textColor="@android:color/black"
    android:textSize="16sp"

    android:textStyle="bold"
    app:layout_constrainedWidth="true"
    app:layout_constraintEnd_toStartOf="@id/message_check_sign"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintStart_toEndOf="@id/img_chat_contact"
    app:layout_constraintTop_toTopOf="@id/img_chat_contact" />

其他回答

你可以用XML写这一行,在这里你取textview:

android:singleLine="true"

我认为你想限制宽度为一行,而不是限制它的字符?由于singleLine已弃用,您可以尝试使用以下组合:

android:maxLines="1"
android:scrollHorizontally="true"
android:ellipsize="end"

我正在使用horizontal Recyclerview。 1)这里在CardView, TextView得到扭曲垂直时使用

android:ellipsize="end"
android:maxLines="1"

检查粗体TextViews Wyman Group, Jaskolski…

2)但是当我使用singleLine和ellipsize -

android:ellipsize="end"
android:singleLine="true"

检查粗体TextViews Wyman Group, Jaskolski…

第二种解决方案对我来说很有效(使用singleLine)。我也在操作系统版本:4.1及以上(直到8.0)进行了测试,它工作得很好,没有任何崩溃。

在你的课文中加上这两行

android:ellipsize="end"
android:singleLine="true"

除了

android:ellipsize="end" 
android:maxLines="1"

你应该设置

android:layout_width="0dp"

也称为“匹配约束”,因为wrap_content值只是将方框展开以适应整个文本,而ellipsize属性无法发挥其作用。