我想在SpannableString下面加上虚线下划线 就像这里是我的字符串
Hello, How are you?
我想在How
字以下开发虚线下划线,以便如何设置它?
如果我添加clickable,它会给我下划线,但我想在How
字下面加上虚线下划线。
谁能帮我 ?
实际上点击那个How
字我打开一个对话框,这样我用可点击的方式实现的东西,但我想用虚线下划线设计How
字。
Editted: 使用下面的帖子点缀下划线,但使用ForegroundColorSpan类不显示文本颜色。任何人都可以帮我解决这个问题。
答案 0 :(得分:2)
你应该使用两个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
}
});
为了让它下划线,你应该使用
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);
}
}
并将此类用于span:
DottedUnderlineSpan dottedUnderlineSpan = new DottedUnderlineSpan(0xFF00FF00, spannedText);
在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);