SpannableString,文本下带有点



我试图实现这样的东西:

有一个字符串的最大长度为 2,最小长度为 1。我想在文本下方用一个点来创建它。像这样的东西 https://i.stack.imgur.com/aV86m.jpg

这可以通过文本视图中的ImageSpan来实现吗?

使用 ReplacementSpan:

public class UnderDotSpan extends ReplacementSpan {
    private final float mSize;
    private final int mDotColor;
    private final int mTextColor;
    public UnderDotSpan(Context context) {
        super();
        TypedArray ta = context.getTheme().obtainStyledAttributes(new int[]{
                R.attr.colorAccent,
                android.R.attr.textColorPrimary
        });
        mDotColor = ta.getColor(0, ContextCompat.getColor(context, R.color.colorAccent));
        mTextColor = ta.getColor(1, 0);
        ta.recycle();
        mSize = context.getResources().getDimension(R.dimen.dot_size);
    }
    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        return Math.round(paint.measureText(text, start, end));
    }
    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        if (TextUtils.isEmpty(text)) {
            return;
        }
        float textSize = paint.measureText(text, start, end);
        paint.setColor(mDotColor);
        canvas.drawCircle(x + textSize / 2, // text center X
                bottom + mSize, // dot center Y
                mSize / 2, // radius
                paint);
        paint.setColor(mTextColor);
        canvas.drawText(text, start, end, x, y, paint);
    }
}

相关内容

  • 没有找到相关文章

最新更新