EditText(TextView同样处理)
1.添加图片主要用SpannableString和ImageSpan类:
Drawable drawable = getResources().getDrawable(R.drawable.icon);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
SpannableString spannable = new SpannableString(getText(R.id.editText1).toString() + "[smile]");
ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
spannable.setSpan(span, getText(R.id.editText1).length(),getText(R.id.editText1).length()+"[smile]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
editText.setText(spannable);
2.将需要的文字高亮显示:
public void highlight(int start,int end){
}
3.加下划线:
public void underline(int start,int end){
}
4.组合运用:
SpannableStringBuilder spannable=new SpannableStringBuilder(getText().toString());
CharacterStyle span_1=new StyleSpan(android.graphics.Typeface.ITALIC);
CharacterStyle span_2=new ForegroundColorSpan(Color.RED);
spannable.setSpan(span_1, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(span_2, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(spannable);
5.带有\n换行符的字符串都可以用此方法显示2种颜色
public SpannableStringBuilder highlight(String text,int color1,int color2,int fontSize){
}