自定义跨度下划线文本与虚线Android

时间:2015-03-31 18:18:11

标签: android dotted-line

我厌倦了用文字中的一个特定单词加下划线,用虚线或虚线表示可点击量。

我找不到解决方案,有人可以帮助我。

SpannableStringBuilder sb = new SpannableStringBuilder(text);    List 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());

1 个答案:

答案 0 :(得分:2)

编辑:我已更新解决方案。有时,没有理由没有调用 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);
    }
}

要在 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

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