备注:各种Span设置
(转自http://blog.csdn.net/harvic880925/article/details/38984705 )
在前面的一个小示例,大家应该也可以看出,要应用一个Span总共分三步:
1、构造String
2、构造Span
3、利用SetSpan()对指定范围的String应用这个Span
1、字体颜色设置(ForegroundColorSpan)
[java] view plain copy
- SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
- //再构造一个改变字体颜色的Span
- ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
- //将这个Span应用于指定范围的字体
- spanString.setSpan(span, 1, 5, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
- //设置给EditText显示出来
- editText.setText(spanString);
效果:
2、字体背景颜色(BackgroundColorSpan)
[java] view plain copy
- SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
- BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);
- spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- editText.setText(spanString);
3、字体大小(AbsoluteSizeSpan)
[java] view plain copy
- SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
- AbsoluteSizeSpan span = new AbsoluteSizeSpan(16);
- spanString.setSpan(span, 2, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- editText.setText(spanString);
4、粗体、斜体(StyleSpan)
[java] view plain copy
- SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
- StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);
- spanString.setSpan(span, 1, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- editText.setText(spanString);
5、删除线(StrikethroughSpan)
[java] view plain copy
- SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
- StrikethroughSpan span = new StrikethroughSpan();
- spanString.setSpan(span, 2, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- editText.setText(spanString);
6、下划线(UnderlineSpan)
[java] view plain copy
- SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
- UnderlineSpan span = new UnderlineSpan();
- spanString.setSpan(span, 1, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- editText.setText(spanString);
7、图片置换(ImageSpan)
ImagSpan有很多构造函数,一般是通过传入Drawableg来构造,详细的构造说明看这里:http://developer.android.com/reference/android/text/style/ImageSpan.html
[java] view plain copy
- SpannableString spanString = new SpannableString("欢迎光临Harvic的博客");
- Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
- d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
- ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
- spanString.setSpan(span, 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- editText.setText(spanString);