textview 中的项目符号点被切断

Sho*_*fle 2 android bulletedlist textview android-layout

我有一个TextView应用程序,其文本由布局中的硬编码字符串资源设置。为了在 中获得项目符号列表TextView,我使用了对<li>元素的(非官方?)支持。这会根据需要创建适当缩进的子弹,但子弹本身的最左侧边缘被略微切断,如您所见:

在这张图片中。

我曾尝试向这些添加左填充,但它对剪切边缘没有任何作用 - 只是将整个东西向内移动。

  1. 有没有简单的解决方案来解决这个问题?
  2. 该项目符号列表的资源在哪里?

Bob*_*ton 5

老问题,但对于其他人来说发现这个晚了:

我发现内置的 BulletSpan 类从早期的 android 版本一直到棉花糖都有错误:

  • 子弹半径和间隙宽度不根据 dp 缩放
  • 子弹有时会被截断(应该是+BULLET_RADIUS,而不是*BULLET_RADIUS)

警告:我见过一些自定义 BulletSpan 类,它们像内部类一样实现 ParcelableSpan。这将导致崩溃并且不打算在外部使用。

这是我的 BulletSpanCompat:

public class BulletSpanCompat implements LeadingMarginSpan {
    private final int mGapWidth;
    private final boolean mWantColor;
    private final int mColor;

    private static final int BULLET_RADIUS = MaterialDesignUtils.dpToPx(1.5f);
    private static Path sBulletPath = null;
    public static final int STANDARD_GAP_WIDTH = MaterialDesignUtils.dpToPx(8);

    public BulletSpanCompat() {
        mGapWidth = STANDARD_GAP_WIDTH;
        mWantColor = false;
        mColor = 0;
    }

    public BulletSpanCompat(int gapWidth) {
        mGapWidth = gapWidth;
        mWantColor = false;
        mColor = 0;
    }

    public BulletSpanCompat(int gapWidth, int color) {
        mGapWidth = gapWidth;
        mWantColor = true;
        mColor = color;
    }

    public BulletSpanCompat(Parcel src) {
        mGapWidth = src.readInt();
        mWantColor = src.readInt() != 0;
        mColor = src.readInt();
    }

    public int getLeadingMargin(boolean first) {
        return 2 * BULLET_RADIUS + mGapWidth;
    }
    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) {
            Paint.Style style = p.getStyle();
            int oldcolor = 0;

            if (mWantColor) {
                oldcolor = p.getColor();
                p.setColor(mColor);
            }

            p.setStyle(Paint.Style.FILL);

            if (c.isHardwareAccelerated()) {
                if (sBulletPath == null) {
                    sBulletPath = new Path();
                    // Bullet is slightly better to avoid aliasing artifacts on mdpi devices.
                    sBulletPath.addCircle(0.0f, 0.0f, 1.2f + BULLET_RADIUS, Path.Direction.CW);
                }

                c.save();
                c.translate(x + dir + BULLET_RADIUS, (top + bottom) / 2.0f);
                c.drawPath(sBulletPath, p);
                c.restore();
            } else {
                c.drawCircle(x + dir + BULLET_RADIUS, (top + bottom) / 2.0f, BULLET_RADIUS, p);
            }

            if (mWantColor) {
                p.setColor(oldcolor);
            }

            p.setStyle(style);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)