6

我有一个TextView存储两行长的地址。这条街在第一行并且是粗体的。城市和州在第二行并且不粗体。如果该行上的单个文本超出,我希望每行的末尾都被省略。我使用 aSpannableString来存储地址,因为我希望街道地址加粗,城市和州不加粗。

有没有一种方法可以做到这一点,而不是每行使用两个而不是完全破解TextViews

示例输出:

例如:
123812 Washington A...
Schenectady New York...

例如:
2792 Dantzler Boulev...
查尔斯顿 SC,29406

例如: 佐治亚州亚特兰大
大街 3 号

4

3 回答 3

21

我有同样的问题,并通过创建一个 EllipsizeLineSpan 类来解决它。你可以用它包裹你想要椭圆化的每一行。

用它标记可跨字符串的示例:

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);
}

椭圆尺寸线跨度:

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);

    }

}


}
于 2013-08-23T20:57:29.713 回答
1

DangVarmit 的回答对我帮助很大,但使用一段时间后,我发现有时文本的顶部偏移量随机不正确。这是需要更换的部分:

  override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fontMetricsInt: Paint.FontMetricsInt?): Int {
    fontMetricsInt?.let {
        it.top = paint.getFontMetricsInt(it)
    }
    return Math.round(paint.measureText(text, start, start))
}
于 2018-02-20T18:24:26.067 回答
-4

尝试TextView在布局中设置椭圆大小,例如:

 android:ellipsize="end"
于 2013-04-03T22:17:21.603 回答