一個TextView使用不一樣的顏色

一個TextView使用不一樣的顏色:ui

textview中首先須要已經有內容了spa

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);
     }
相關文章
相關標籤/搜索