ForegroundColorSpan

字体颜色样式。

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

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

示例1:

ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);

String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(foregroundColorSpan, 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);
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(parcel);

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

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

示例3:

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

ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(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(foregroundColorSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

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