Programmatically create TextView with ellipsis

后端 未结 1 1364
星月不相逢
星月不相逢 2020-12-03 13:10

I\'m programmatically creating a TextView that I want to ellipsis at the end.

pseudo code:

    tv.setEllipsize(TextUtils.TruncateAt.END);
    tv.setH         


        
相关标签:
1条回答
  • 2020-12-03 14:04

    setSingleLine() or setSingleLine(true) prevents the TextView from changing its height to more lines and forces the TextView to ignore line breaks (the symbol \n in a string).

    setMaxLines(int n) displays the first n lines of the String displayed in the TextView which are separated by a line break.

    For example let the String be "my first line \n and my second line \n and a third one"

    • setSingleLine() lets the TextView display "my first line and my.." since the display width is exceeded and
    • setMaxLines(1) results in "my first line"
    • setMaxLines(2) results in "my first line" and below a line saying "and my second line"
    • setMaxLines(3) obviously does not have any effect on this sample string.

    Update: This should work for "setDoubleLine with truncation":

    // optional: string.replace("\n",""); or string.replace("\n"," ");
    tv.setSingleLine(false);
    tv.setEllipsize(TextUtils.TruncateAt.END);
    int n = 2; // the exact number of lines you want to display
    tv.setLines(n);
    
    0 讨论(0)
提交回复
热议问题