BackgroundColorSpan

背景色样式。

BackgroundColorSpan(int color):参数color,颜色值。

BackgroundColorSpan(Parcel src):参数src,包含颜色值信息的包装类。

示例1:

BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED);

String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(backgroundColorSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);

示例2:

Parcel parcel = Parcel.obtain();
parcel.writeInt(Color.RED);
parcel.setDataPosition(0);
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(parcel);

String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(backgroundColorSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);

示例3:

重写updateDrawState(TextPaint textPaint)方法,修改删除线的样式、文本颜色:

BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED) {
    @Override
    public void updateDrawState(TextPaint textPaint) {
        textPaint.setColor(Color.argb(255, 54, 92, 124));
    }
};

String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(backgroundColorSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);