一个TextView使用不同的颜色:
textview中首先需要已经有内容了
public static void ColorFormatTextView(TextView tv,int color,String textStr){ if (TextUtils.isEmpty(textStr) || tv == null || TextUtils.isEmpty(tv.getText())) { return; } String showString = tv.getText().toString(); int startIndex = showString.indexOf(textStr); if (startIndex < 0) { return; } ForegroundColorSpan style = new ForegroundColorSpan(color); SpannableStringBuilder formatted = new SpannableStringBuilder( showString); formatted.setSpan(style, startIndex, startIndex + textStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE); tv.setText(formatted); }
时间: 2024-10-03 19:32:28