android中android.text包
1.点亮文字
TextView tv=(TextView)findViewById(R.id.textView1);
String str="thisisatest";
SpannableStringBuilder style=new SpannableStringBuilder(str); //将str字符串载入SpannableStringBuilder对象中
style.setSpan(new ForegroundColorSpan(Color.RED), 1, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv.setText(style);
2.加下划线
TextView tv=(TextView)findViewById(R.id.textView1);
String str="thisisatest";
SpannableStringBuilder style=new SpannableStringBuilder(str); //将str字符串载入SpannableStringBuilder对象中
CharacterStyle span=new UnderlineSpan();
style.setSpan(span, 1, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv.setText(style);
3.插入表情
TextView tv=(TextView)findViewById(R.id.textView1);
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
String str="this is my test";
SpannableString spannable = new SpannableString(str);
String rex = " ";
Pattern pattern = Pattern.compile(rex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
//要让图片替代指定的文字就要用ImageSpan
ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
//开始替换
spannable.setSpan(span, matcher.start(),matcher.end(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
tv.setText(spannable);