微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐