gpt4 book ai didi

android - 带有虚线 Android 的自定义跨度下划线文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:15:50 25 4
gpt4 key购买 nike

我厌倦了用点线或虚线以及 Clickable Span 在文本中的特定单词下划线。

我还没有找到解决方案,请有人帮助我。

SpannableStringBuilder sb = new SpannableStringBuilder(text); 列表 listmot = new ArrayList();

       listmot=db.getAlldef();

for(int i=0;i<listmot.size();i++)
{


String mot = (listmot.get(i)).get_mot();
final String def = (listmot.get(i)).get_definition();

Log.v(null, mot);
Pattern p = Pattern.compile(mot, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(text);
while (m.find()){


// Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/font.ttf");
//sb.setSpan(new CustomTypefaceSpan("",tf), m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

sb.setSpan(new ClickableSpan() {

@Override
public void onClick(View widget) {
// Toast.makeText(getApplicationContext(), def,Toast.LENGTH_LONG).show();



int[] values = new int[2];
widget.getLocationOnScreen(values);
Log.d("X & Y",values[0]+" "+values[1]);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AndroidSQLiteTutorialActivity.this);
alertDialog.setMessage(def);
alertDialog.setCancelable(true);

AlertDialog alert11 = alertDialog.create();
alert11.setCanceledOnTouchOutside(true);

alert11.show();

}
}, m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

}

}



textjson.setText(sb);
textjson.setMovementMethod(LinkMovementMethod.getInstance());

最佳答案

编辑:我已经更新了解决方案。有时,在使用 LineBackgroundSpan 的解决方案中,drawBackground 方法只是无缘无故地没有被调用。新版本一直有效,看起来更清晰。

我遇到了同样的问题,我通过结合这些解决方案解决了它:

结果代码如下所示:

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);
}
}

要使您的下划线在所有密度上看起来都相同,请将 dimens 设置为 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);

这就是你如何使用它:

  DottedUnderlineSpan dottedUnderlineSpan = new DottedUnderlineSpan(0xFF00FF00, spannedText);
spannableString.setSpan(dottedUnderlineSpan, startOfText, endOfText, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

并确保关闭将显示下划线范围的 textView 上的硬件加速https://stackoverflow.com/a/24467362/5373069

编辑:如果您看到改进此解决方案的方法,我将不胜感激。

关于android - 带有虚线 Android 的自定义跨度下划线文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29375638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com