android中TextView的SpannableString小结

我们使用QQ,微信的时候,会发现:其文本样式多种多样,可以在TextView中显示表情,图片,超链接等等。今天就来大致总结一下相关的样式。

先看代码:

[java]  view plain copy
  1. /** 
  2.          * 设置前景色 
  3.          * @param tv 
  4.          */  
  5.         private void setForegroundColorSpan(TextView tv) {    
  6.             SpannableString spanString = new SpannableString("前景色textview");    
  7.             ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);    
  8.             spanString.setSpan(span, 03, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
  9.             tv.append(spanString);    
  10.         }    
  11.           
  12.         /** 
  13.          * 设置背景色 
  14.          * @param tv 
  15.          */  
  16.         private void setBackgroundColorSpan(TextView  tv) {    
  17.             SpannableString spanString = new SpannableString("背景色textview");    
  18.             BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);    
  19.             spanString.setSpan(span, 03, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
  20.             tv.append(spanString);    
  21.         }   
  22.         /** 
  23.          * 设置粗体样式 
  24.          * @param tv 
  25.          */  
  26.         private void setStyleSpan(TextView tv) {    
  27.             SpannableString spanString = new SpannableString("粗体斜体textview");    
  28.             StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);    
  29.             spanString.setSpan(span, 04, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
  30.             tv.append(spanString);    
  31.         }    
  32.         /** 
  33.          * 设置字体大小 
  34.          * @param tv 
  35.          */  
  36.         private void setRelativeFontSpan(TextView tv) {  
  37.             SpannableString spanString = new SpannableString("字体相对大小textview");  
  38.             spanString.setSpan(new RelativeSizeSpan(2.5f), 06,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);  
  39.             tv.append(spanString);      
  40.         }  
  41.         /** 
  42.          * 设置文本字体 
  43.          * @param tv 
  44.          */  
  45.         private void setTypefaceSpan(TextView tv) {  
  46.             SpannableString spanString = new SpannableString("文本字体textview");  
  47.             spanString.setSpan(new TypefaceSpan("monospace"), 04, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  48.             tv.append(spanString);  
  49.         }  
  50.         /** 
  51.          * 添加URL超链接 
  52.          * @param tv 
  53.          */  
  54.         private void addUrlSpan(TextView tv) {    
  55.             SpannableString spanString = new SpannableString("csdntextview");    
  56.             URLSpan span = new URLSpan("http://blog.csdn.net/nuptboyzhb");    
  57.             spanString.setSpan(span, 04, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
  58.             tv.append(spanString);  
  59.             tv.setMovementMethod(LinkMovementMethod.getInstance());  
  60.         }    
  61.         /** 
  62.          * 在文字中显示图片表情等 
  63.          * @param tv 
  64.          */  
  65.         private void addImageSpan(TextView tv) {    
  66.             SpannableString spanString = new SpannableString("文中有图片表情等textview");    
  67.             Drawable d = getResources().getDrawable(R.drawable.ic_launcher);    
  68.             d.setBounds(00, d.getIntrinsicWidth(), d.getIntrinsicHeight());    
  69.             ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);    
  70.             spanString.setSpan(span, 01, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
  71.             tv.append(spanString);    
  72.         }   
  73.         /** 
  74.          * 点击后跳转Activity 
  75.          * @param textView 
  76.          * @param packageContext 
  77.          * @param cls 
  78.          */  
  79.         private void clickableSpan(TextView textView,final Context packageContext,final Class<?> cls){  
  80.             String text = "显示Activity";  
  81.             SpannableString spannableString = new SpannableString(text);  
  82.             spannableString.setSpan(new ClickableSpan() {  
  83.                 @Override  
  84.                 public void onClick(View widget) {  
  85.                     Intent intent = new Intent(packageContext,cls);  
  86.                     startActivity(intent);  
  87.                 }  
  88.                 // 表示点击整个text的长度都有效触发这个事件  
  89.             }, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  90.             textView.setText(spannableString);  
  91.             textView.setMovementMethod(LinkMovementMethod.getInstance());  
  92.         }  
  93.         /** 
  94.          * 添加下划线 
  95.          * @param tv 
  96.          */  
  97.         private void addUnderLineSpan(TextView tv) {    
  98.             SpannableString spanString = new SpannableString("下划线textview");    
  99.             UnderlineSpan span = new UnderlineSpan();    
  100.             spanString.setSpan(span, 03, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
  101.             tv.append(spanString);    
  102.         }   
  103.         /** 
  104.          * 添加删除线样式 
  105.          * @param tv 
  106.          */  
  107.         private void addStrikeSpan(TextView tv) {    
  108.             SpannableString spanString = new SpannableString("删除线textview");    
  109.             StrikethroughSpan span = new StrikethroughSpan();    
  110.             spanString.setSpan(span, 03, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
  111.             tv.append(spanString);    
  112.         }    


效果截图:

android中TextView的SpannableString小结_第1张图片


更多样式,参见官方文档。

http://developer.android.com/reference/android/text/style/package-summary.html

你可能感兴趣的:(SpannableString)