Android 设置部分的字体的颜色

一 可以直接调用Html.fromhtml()的方法,这样就可以直接使用Html里的标签

例如:TextView txt=new TextView(this);

txt.setText(Html.fromhtml("你好啊!<font color=red>你也好啊!</font>"));

二 使用SpannableStringBuilder类进行设置

String str="你好啊!你也好啊"

SpannableStringBuilder style=new SpannableStringBuilder(str);

style.setSpan(new ForegroundColorSpan(Color.RED),0,4,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

txt.setText(str);

注:上面“0,4”表示的是从字符串的第0位开始到第四位的字设置设置成RED(红色)

原文地址:https://www.cnblogs.com/jita/p/2828906.html