android中TextView的SpannableString小结
来源:互联网 发布:php论坛源码下载 编辑:程序博客网 时间:2024/05/21 09:58
我们使用QQ,微信的时候,会发现:其文本样式多种多样,可以在TextView中显示表情,图片,超链接等等。今天就来大致总结一下相关的样式。
先看代码:
-
-
-
-
- private void setForegroundColorSpan(TextView tv) {
- SpannableString spanString = new SpannableString("前景色textview");
- ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
- spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- tv.append(spanString);
- }
-
-
-
-
-
- private void setBackgroundColorSpan(TextView tv) {
- SpannableString spanString = new SpannableString("背景色textview");
- BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);
- spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- tv.append(spanString);
- }
-
-
-
-
- private void setStyleSpan(TextView tv) {
- SpannableString spanString = new SpannableString("粗体斜体textview");
- StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);
- spanString.setSpan(span, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- tv.append(spanString);
- }
-
-
-
-
- private void setRelativeFontSpan(TextView tv) {
- SpannableString spanString = new SpannableString("字体相对大小textview");
- spanString.setSpan(new RelativeSizeSpan(2.5f), 0, 6,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
- tv.append(spanString);
- }
-
-
-
-
- private void setTypefaceSpan(TextView tv) {
- SpannableString spanString = new SpannableString("文本字体textview");
- spanString.setSpan(new TypefaceSpan("monospace"), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- tv.append(spanString);
- }
-
-
-
-
- private void addUrlSpan(TextView tv) {
- SpannableString spanString = new SpannableString("csdntextview");
- URLSpan span = new URLSpan("http://blog.csdn.net/nuptboyzhb");
- spanString.setSpan(span, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- tv.append(spanString);
- tv.setMovementMethod(LinkMovementMethod.getInstance());
- }
-
-
-
-
- private void addImageSpan(TextView tv) {
- SpannableString spanString = new SpannableString("文中有图片表情等textview");
- 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, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- tv.append(spanString);
- }
-
-
-
-
-
-
- private void clickableSpan(TextView textView,final Context packageContext,final Class<?> cls){
- String text = "显示Activity";
- SpannableString spannableString = new SpannableString(text);
- spannableString.setSpan(new ClickableSpan() {
- @Override
- public void onClick(View widget) {
- Intent intent = new Intent(packageContext,cls);
- startActivity(intent);
- }
-
- }, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- textView.setText(spannableString);
- textView.setMovementMethod(LinkMovementMethod.getInstance());
- }
-
-
-
-
- private void addUnderLineSpan(TextView tv) {
- SpannableString spanString = new SpannableString("下划线textview");
- UnderlineSpan span = new UnderlineSpan();
- spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- tv.append(spanString);
- }
-
-
-
-
- private void addStrikeSpan(TextView tv) {
- SpannableString spanString = new SpannableString("删除线textview");
- StrikethroughSpan span = new StrikethroughSpan();
- spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- tv.append(spanString);
- }
效果截图:
更多样式,参见官方文档。
http://developer.android.com/reference/android/text/style/package-summary.html
0 0