Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

android - Programmatically create TextView with ellipsis

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

pseudo code:

    tv.setEllipsize(TextUtils.TruncateAt.END);
    tv.setHorizontallyScrolling(false);
    tv.setSingleLine();

The above works GREAT.

    tv.setEllipsize(TextUtils.TruncateAt.END);
    tv.setHorizontallyScrolling(false);
    tv.setMaxLines(1);

This does not work. Is this a bug? I don't understand why I can't get text to ellipses at the end when specifying maxLines especially a maxLine of 1 but setSingleLine is ok.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

setSingleLine() or setSingleLine(true) prevents the TextView from changing its height to more lines and forces the TextView to ignore line breaks (the symbol 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 and my second line 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("
",""); or string.replace("
"," ");
tv.setSingleLine(false);
tv.setEllipsize(TextUtils.TruncateAt.END);
int n = 2; // the exact number of lines you want to display
tv.setLines(n);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...