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>

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...