[英]Dotted underline in TextView using SpannableString in Android
I want dotted underline below SpannableString Like here is my string 我想在SpannableString下面用虚线下划线像这是我的字符串
Hello, How are you?
I want to develop dotted underline below How
word so how to set it? 我想在下面开发虚线下划线。 How
设置单词呢? If i add clickable that its give me underline but i want dotted underline below How
word. 如果我添加clickable,它会给我加下划线,但是我想在How
word下加点划线。 Can anyone help me ? 谁能帮我 ? Actually on click of that How
word i am opening a dialog so that thing i achieved with clickable but i want to design that How
word with dotted underline. 实际上,在单击该How
word时,我正在打开一个对话框,以便我用clickable实现该功能,但是我想设计带有虚线下划线的How
word。
Editted: Using below post dotted underline appears but text color is not showing using ForegroundColorSpan class. 编辑:在下面的帖子中使用虚线下划线出现,但是使用ForegroundColorSpan类未显示文本颜色。 can anybody help me to resolve this issue. 谁能帮助我解决这个问题。
You should use two SpannableString
for it as 您应该使用两个SpannableString
作为
SpannableString ss = new SpannableString("Hello, how are you ?");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
//perform operation on click
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
ss.setSpan(clickableSpan, 0, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(getActivity().getResources().getColor(android.R.color.holo_blue_light)), 0, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView_understand.setText(ss);
textView_understand.setMovementMethod(LinkMovementMethod.getInstance());
textView_understand.setHighlightColor(Color.TRANSPARENT);
textView_understand
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//On click
}
});
And for make it dotted underline you should use 为了使它下划线,您应该使用
private static class DottedUnderlineSpan extends ReplacementSpan {
private Paint p;
private int mWidth;
private String mSpan;
private float mSpanLength;
private boolean mLengthIsCached = false;
public DottedUnderlineSpan(int _color, String _spannedText){
p = new Paint();
p.setColor(_color);
p.setStyle(Paint.Style.STROKE);
p.setPathEffect(new DashPathEffect(new float[]{mDashPathEffect, mDashPathEffect}, 0));
p.setStrokeWidth(mStrokeWidth);
mSpan = _spannedText;
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
mWidth = (int) paint.measureText(text, start, end);
return mWidth;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
canvas.drawText(text, start, end, x, y, paint);
if(!mLengthIsCached)
mSpanLength = paint.measureText(mSpan);
// https://code.google.com/p/android/issues/detail?id=29944
// canvas.drawLine can't draw dashes when hardware acceleration is enabled,
// but canvas.drawPath can
Path path = new Path();
path.moveTo(x, y + mOffsetY);
path.lineTo(x + mSpanLength, y + mOffsetY);
canvas.drawPath(path, this.p);
}
}
and use this class for span as: 并使用此类的跨度为:
DottedUnderlineSpan dottedUnderlineSpan = new DottedUnderlineSpan(0xFF00FF00, spannedText);
To make your underline look the same on all densities set that dimens in dp 为了使下划线在dp中变暗的所有密度集上看起来都相同
mStrokeWidth = context.getResources().getDimension(R.dimen.stroke_width);
mDashPathEffect = context.getResources().getDimension(R.dimen.dash_path_effect);
mOffsetY = context.getResources().getDimension(R.dimen.offset_y);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.