美文网首页
android 局部文字变色

android 局部文字变色

作者: 小婷婷tt | 来源:发表于2018-05-16 13:43 被阅读0次

public static SpannableString getColorTextByKeywords(String text,int colorId, String keyword) {

SpannableString ss =new SpannableString(text);

int start = text.indexOf(keyword);

int end = start + keyword.length();

if (start != -1 && end != -1) {

//用颜色标记文本

        ss.setSpan(new ForegroundColorSpan(colorId), start, end,

//setSpan时需要指定的 flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括).

                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

}

return ss;

}

调用举例:tv_name.setText(StringUtil.getColorTextByKeywords(name,mContext.getResources().getColor(R.color.tabtext_color),mKey));

相关文章

网友评论

      本文标题:android 局部文字变色

      本文链接:https://www.haomeiwen.com/subject/udepdftx.html