我有一个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"/>

当前回答

添加“…”,如果太长的话,就在文章结尾:

检查文本宽度是否恒定 把这两行加起来 android: ellipsize = "结束" android: maxLines =“1”

约束布局中的textview的完整代码:

<TextView
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:textSize="15sp"
        android:textAllCaps="false"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent" />

其他回答

您需要为textview的布局添加以下行

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

希望这对你有用。

弃用:

在你的Textview中添加一个属性android:singleLine="true"

更新:

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

设置android:maxLength="8"在Textview

如果你想设置你的字符串>5。然后设置textview长度5+3 (3 -dot)

if (yourString.length()>5) // 
    {
        textview.setText(yourString.substring(0,5)+"...");
    }
    else {
        textview.setText(title);
    }

@AzharShaikh的方法很有效。

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

但我意识到一个麻烦,TextView将被字截断(默认情况下)。显示我们是否有这样的文本:

测试long_line_without_any_space_abcdefgh

TextView将显示:

测试……

我找到了解决方案来处理这个问题,用unicode无间断空格字符替换空格,它使TextView包装字符而不是单词:

yourString.replace(" ", "\u00A0");

结果:

测试long_line_without_any_space_abc……

你可以通过xml:

<TextView 
    android:id="@+id/textview"
    android:maxLines="1" // or any number of lines you want
    android:ellipsize="end"
    />