0

我正在尝试实现这样的目标:

有一个最大长度为 2 且最小长度为 1 的字符串。我想在文本下方创建一个点。像这样http://imgur.com/7LKWpVR

这可以通过 textview 中的 ImageSpan 来实现吗?

4

1 回答 1

0

使用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);
    }
}
于 2016-03-16T15:33:50.737 回答