简体   繁体   English

SpannableString在文本下带有点

[英]SpannableString with dot under text

I am trying to achive something like this : 我正在尝试达到这样的目标:

There is a string with maximal lenght of 2 and minimal lenght of 1. I would like to create it with a dot under the text. 有一个最大长度为2且最小长度为1的字符串。我想在文本下方添加一个点。 Something like this http://imgur.com/7LKWpVR 像这样的东西http://imgur.com/7LKWpVR

Can this be achived with ImageSpan in textview ? 可以在Textview中使用ImageSpan实现吗?

Use a ReplacementSpan : 使用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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM