文本下方带点的 SpannableString
SpannableString with dot under text
我正在努力实现这样的目标:
有一个最大长度为 2,最小长度为 1 的字符串。我想在文本下方创建一个点。像这样 http://imgur.com/7LKWpVR
这可以用 textview 中的 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);
}
}
我正在努力实现这样的目标:
有一个最大长度为 2,最小长度为 1 的字符串。我想在文本下方创建一个点。像这样 http://imgur.com/7LKWpVR
这可以用 textview 中的 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);
}
}