Android, How to limit width of TextView (and add three dots at the end of text)?

前端 未结 21 1932
野的像风
野的像风 2020-12-02 03:44

I have a TextView that I want to limit characters of it. Actually, I can do this but the thing that I\'m looking for is how to add three dots (...) at the end o

21条回答
  •  甜味超标
    2020-12-02 04:18

    The following is what I learned by playing around with various options for forcing a TextView to a single line (with and without the three dots).

    enter image description here

    android:maxLines="1"

    
    

    This just forces the text to one line. Any extra text is hidden.

    Related:

    • android:maxLines
    • android:singleLine (Note this and this)
    • android:lines

    ellipsize="end"

    
    

    This cuts off the text that doesn't fit but lets users know that the text has been truncated by adding an ellipsis (the three dots).

    Related:

    • ellipsize="start" (...aaabbbccc)
    • ellipsize="middle" (aaa...ccc)
    • android: Ellipsise , meaning of the options

    ellipsize="marquee"

    
    

    This makes the text scroll automatically across the TextView. Note that sometimes it needs to be set in code:

    textView.setSelected(true);
    

    Supposedly android:maxLines="1" and android:singleLine="true" should do basically the same thing and since singleLine is apparently deprecated I would prefer not to use it, but when I take it out, the marquee doesn't scroll anymore. Taking maxLines out doesn't affect it, though.

    Related:

    • Marquee text in Android

    HorizontalScrollView with scrollHorizontally

    
    
        
    
    

    This allows the user to manually scroll to see the whole line of text.

提交回复
热议问题