textView 文字显示不同颜色

一下是具体用到的代码:

SpannableStringBuilder builder = new SpannableStringBuilder(message+message2);

ForegroundColorSpan redSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.text_red_color));

ForegroundColorSpan graySpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.color_c));

builder.setSpan(redSpan, 0, message.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

 int timeStart = message.length() + 1 + message2.length() ;

 int timeEnd = timeStart + time.length();

 builder.setSpan(graySpan, timeStart, timeEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

 textView.setText(builder);

你可能感兴趣的:(Android,工具类)