Android中对TextView中的部分内容的字体样式的设置方法

Android中的TextView中内容,有时候需要对其部分内容添加下划线和颜色操作:

String str = "回复 " + uname + " 的评论: " + "该评论已删除!";
SpannableStringBuilder msp = new  SpannableStringBuilder(str);
msp.setSpan(new ForegroundColorSpan(0xff267ec2), 3, uname.length()+3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体颜色
msp.setSpan(new ForegroundColorSpan(0xff267ec2), 3+uname.length()+6, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体颜色
msp.setSpan(new UnderlineSpan(), 3, uname.length()+3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //添加下划线
twoTxt.setText(msp);


原文地址:https://www.cnblogs.com/roccheung/p/5797422.html