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);