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

前端 未结 21 1826
野的像风
野的像风 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:29

    The approach of @AzharShaikh works fine.

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

    But I realize a trouble that TextView will be truncated by word (in default). Show if we have a text like:

    test long_line_without_any_space_abcdefgh

    the TextView will display:

    test...

    And I found solution to handle this trouble, replace spaces with the unicode no-break space character, it makes TextView wrap on characters instead of words:

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

    The result:

    test long_line_without_any_space_abc...

提交回复
热议问题