如何添加文字下面的红色波浪线在Android的TextView中 [英] How to add red wavy line below text in Android's TextView

查看:2314
本文介绍了如何添加文字下面的红色波浪线在Android的TextView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想补充以下文本中的错误,如红色波浪线:

I'm trying to add red wavy line below errors in texts, such as:

可惜我不能找到一个妥善的*跨度类来包装与错误文本。

Unfortunately I can't find a proper *Span class to wrap the error text with.

我应该如何在Android中实现这样的功能?

How should I implement such a feature in Android?

推荐答案

我已经通过实现自定义跨度解决了这个问题:

I've solved the problem by implementing a custom Span:

添加 error_underline.png 来你的资源:< - 在这里小小的6X3像素

Add error_underline.png to your resources: <-- tiny 6x3 pixels here

然后使用这个类来创建跨度:

Then use this class to create spans:

static class ErrorSpan extends DynamicDrawableSpan {

    private BitmapDrawable mRedWavy;
    private int mWidth;
    private int mBmpHeight;

    ErrorSpan(Resources resources) {
        super(DynamicDrawableSpan.ALIGN_BASELINE);
        mRedWavy = new BitmapDrawable(resources, BitmapFactory.decodeResource(resources, R.drawable.error_underline));
        mBmpHeight = mRedWavy.getIntrinsicHeight();
        mRedWavy.setTileModeX(TileMode.REPEAT);
    }

    @Override
    public Drawable getDrawable() {
        return mRedWavy;
    }

    @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) {

        mRedWavy.setBounds(0, 0, mWidth, mBmpHeight);
        canvas.save();
        canvas.translate(x, bottom-mBmpHeight);
        mRedWavy.draw(canvas);
        canvas.restore();
        canvas.drawText(text.subSequence(start, end).toString(), x, y, paint);
    }
}

这篇关于如何添加文字下面的红色波浪线在Android的TextView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆