将点添加到Textview(...)的末尾,点不显示

时间:2019-05-18 11:37:31

标签: android textview

每当文本(我通过“活动”通过编程方式设置的)文本长于 n 个字符时,我要删减并限制文本的长度,并在{{1 }}。最初在textview中将文本设置为空(只是“”),稍后再通过活动进行设置。

这是我尝试的方式:

xml

这是它最终显示随机文本的方式,我将maxlength设置为10个字符:

enter image description here

我期望它看起来像<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:maxLength="10" android:ellipsize="end" android:maxLines="1"/> ,因为文本是jrkfkgkglg...,但是末尾不会添加点。有什么建议吗?

3 个答案:

答案 0 :(得分:2)

要使其正常工作,您的宽度必须为match_parent或已定义的任何其他尺寸,而不是wrap_content

答案 1 :(得分:1)

您正在告诉您的编辑文本,最大长度为10个字符,在屏幕截图中,您有一个具有该长度的字符串,因此,如果您尝试向该字符串中添加任何内容,都不会被添加。

您可以删除android:maxLength="10"限制并以编程方式从代码中检查文本,如下所示:

if (yourText.length() > 10) {
    yourText = yourText.substring(0, 9); //get the first 10 chars
    textView.setText(yourText + "..."); //display 
} else {
    textView.setText(original string < 10chars);
}

答案 2 :(得分:1)

为此,您必须删除属性

  

android:maxLines =“ 1”

并添加新属性

  

android:singleLine =“ true”

,此外还使您的textview宽度为match_parent。 当您添加它时,它将正常工作,然后您无需使用其他任何东西。