我有同樣的問題,並通過創建一個EllipsizeLineSpan類解決了這個問題。你可以用你的橢圓大小來包裝每一行。用於標記一個spannable字符串,它
例子:
SpannableStringBuilder textspan = new SpannableStringBuilder("#1.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n"+
"Protect from ellipsizing #2.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n"+
"#3.Lorem ipsum dolor sit amet, consectetur adipisicing elit\n"+
"#4.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n");
// find ellipsizable text (from '#' to newline)
Pattern pattern = Pattern.compile("#.*\\n", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(textspan);
while(matcher.find()) {
textspan.setSpan(new EllipsizeLineSpan(), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
EllipsizeLineSpan:
public class EllipsizeLineSpan extends ReplacementSpan implements LineBackgroundSpan {
int layoutLeft = 0;
int layoutRight = 0;
public EllipsizeLineSpan() {
}
@Override
public void drawBackground (Canvas c, Paint p,
int left, int right,
int top, int baseline, int bottom,
CharSequence text, int start, int end,
int lnum) {
Rect clipRect = new Rect();
c.getClipBounds(clipRect);
layoutLeft = clipRect.left;
layoutRight = clipRect.right;
}
@Override
public int getSize (Paint paint, CharSequence text, int start, int end,
Paint.FontMetricsInt fm) {
return layoutRight - layoutLeft;
}
@Override
public void draw (Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, Paint paint) {
float textWidth = paint.measureText(text, start, end);
if (x + (int) Math.ceil(textWidth) < layoutRight) { //text fits
canvas.drawText(text, start, end, x, y, paint);
} else {
float ellipsiswid = paint.measureText("\u2026");
// move 'end' to the ellipsis point
end = start + paint.breakText(text, start, end, true, layoutRight - x - ellipsiswid, null);
canvas.drawText(text, start, end, x, y, paint);
canvas.drawText("\u2026", x + paint.measureText(text, start, end), y, paint);
}
}
}
但是,這隻會省略整個TextView的末端。我希望兩行都被省略。 – erin 2013-04-04 05:15:34
@erin我不認爲用一個'TextView'就可以,放幾個'TextView'就是一個竅門。 – NullPointer 2013-04-04 09:01:41