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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…