android 字符串特定字符變色

先上效果:
在這裏插入圖片描述

代碼實現:
text 數據源
keyword 要變顏色的字符串
color_FA9A3A 要變的顏色
style_color_FA9A3A 也可以改變字體的size和其他的熟悉,自己設置

    public SpannableString matcherSearchText( String text, String keyword) {
        SpannableString ss = new SpannableString(text);
        Pattern pattern = Pattern.compile(keyword);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new TextAppearanceSpan(this, R.style.style_color_FA9A3A), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//new ForegroundColorSpan(color)
        }
        return ss;
    }
   <style name="style_color_FA9A3A">
        <item name="android:textColor">@color/color_FA9A3A</item>
    </style>
  <color name="color_FA9A3A">#FA9A3A</color>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章