如果TextView的内容超过一行,我该如何在其上显示省略号?

125
我有一个不起作用的Layout如下:
<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:id="@+id/experienceLayout" 
    android:background="#ffffff" 
    android:layout_height="match_parent" 
    android:paddingLeft="6dp" 
    android:paddingRight="6dp" 
    android:paddingBottom="6dp" 
    android:paddingTop="6dp">

    <TextView 
        android:layout_weight="1" 
        android:id="@+id/experienceLabel" 
        android:text="Experience" 
        android:layout_height="wrap_content" 
        android:textColor="#000000" 
        android:layout_width="wrap_content" 
        android:textStyle="bold">
    </TextView>

    <TextView 
        android:id="@+id/experienceTextView" 
        android:text="TextView" 
        android:layout_height="wrap_content" 
        android:textColor="#000000" 
        android:layout_width="wrap_content" 
        android:ellipsize="end" 
        android:lines="1" 
        android:maxLines="1" 
        android:singleLine="true" 
        android:fadeScrollbars="false">
    </TextView>

</LinearLayout>
7个回答

339

这是一个常见的问题。请尝试使用以下方法:

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

滚动条水平滚动是使其起作用的“特殊配方”。


13
奇怪...我尝试使用android:scrollHorizontally="true",但它不起作用,我不得不使用已弃用的属性android:singleLine="true" - Gerardo Contijoch
1
是的,scrollHorizontally 是实际的关键字,用于 ... - Rishabh Dutt Sharma
2
scrollHorizontally?显然我不想要水平滚动效果。 - filthy_wizard
1
@用户1232726:是的,“水平滚动”。就常识而言,请查看问题的日期和回答的日期…毫无疑问,两者都可能不再相关(在发表评论之前)。 - BonanzaDriver
1
不需要scrollHorizontally属性即可工作。 - jegadeesh
显示剩余2条评论

38

这也会使一行带有省略号

 android:singleLine="true"

16
显然,这已经被弃用了。 - Oliver Pearmain
1
这个更好地打破长单词。 - bpiec
android:maxLines="1" - Jaydev
1
@grebulon 它已被弃用。至少现在是这样。 - Spikatrix

25

使用此功能

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

在完全了解输出内容之前,请勿使用此功能

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

当您使用 maxlines = 1 时,有时会截断大部分字符。


18

在多个设备/ API 上,对我有效的编程方式如下(其中 tv 是您的 TextView):

    if (tv.getLineCount() > 1) {
        int lineEndIndex = tv.getLayout().getLineEnd(0);
        String text = tv.getText().subSequence(0, lineEndIndex - 3) + "\u2026";
        tv.setText(text);
    }

2
这是所有答案中最有帮助的...适用于每个API,并且可以轻松地将其转换为Utils库。 - Mariano Zorrilla
1
你应该使用省略号字符\u2026,而不是三个.字符。 - Chris Stillwell
你说得对,@ChrisStillwell。我在我的代码中确实使用省略号字符。我已经编辑了答案,谢谢。 :) - Marilia

4

以上所有答案都符合只显示1行文本并添加省略号的要求。但是,如果您希望在某些文本行后出现省略号,则应使用以下方法:

android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"

这样做后,省略号仅在两行后出现。注意:将singleLine设置为false非常重要。


根据文档,该属性的默认值为false:https://developer.android.com/reference/android/widget/TextView#attr_android:singleLine - Pierre

3

2

android:singleLine已经被弃用。在我的情况下,我需要为TextView获取一个固定的高度,并且我使用了android:lines属性来替代android:maxLines。我认为这可能会帮助到和我有相同问题的人。

android:ellipsize="end"
android:lines="2"

不是真的。文档中没有关于弃用的说明:https://developer.android.com/reference/android/widget/TextView#attr_android:singleLine - cesards
1
@cesards 有意思。这份文档显示它是 - https://developer.android.com/reference/android/R.attr.html#singleLine - Reaz Murshed
1
@cesards,根据Android in Code文档:该(android:singleLine)属性已被弃用。使用maxLines来更改静态文本的布局,并在inputType属性中为可编辑文本视图使用textMultiLine标志(如果提供了singleLine和inputType,则inputType标志将覆盖singleLine的值)。 - Neckster

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接