1

BulletSpan通过扩展LeadingMarginSpan以在TextView. 的文档LeadingMarginSpan说它可以嵌套(即多级项目符号):

单个段落可以有多个前导边距跨度;它们将按顺序呈现,每个都将其边距添加到它之前的边距。

该类LeadingMarginSpan有这个方法:

public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
                              int top, int baseline, int bottom,
                              CharSequence text, int start, int end,
                              boolean first, Layout l);

我不知道方法的实现是应该自己计算缩进还是已经缩进。跨设备有不同的行为。例如,在API 级别 21中,它x始终以 0 调用,在API 级别 23中,它针对不同级别以不同的方式调用x

那么,实现此方法的正确方法是什么?

当前的实现很像原来的BulletSpan

    @Override
    public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
                                  int top, int baseline, int bottom,
                                  CharSequence text, int start, int end,
                                  boolean first, Layout l) {
        if (((Spanned) text).getSpanStart(this) == start) {
            ...
            if (c.isHardwareAccelerated()) {
                ...
                c.translate(x + dir * mBulletRadius,
                        (top + bottom) / 2.0f);
                ...
            } else {
                c.drawCircle(x + dir * mBulletRadius,
                        (top + bottom) / 2.0f, mBulletRadius, p);
            }
            ...
        }
        ...
    }

API 级别 21中的行为:

o  Level 1
o     Level 2
o        Level 3
o        Level 3
o     Level 2

API 级别 23中的行为:

o  Level 1
   o  Level 2
      o  Level 3
      o  Level 3
   o  Level 2

提前致谢

4

0 回答 0