android中TextView的SpannableString小结

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

先看代码:

/**
		 * 设置前景色
		 * @param tv
		 */
		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);  
		}  
		
		/**
		 * 设置背景色
		 * @param tv
		 */
		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);  
		} 
		/**
		 * 设置粗体样式
		 * @param tv
		 */
		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);  
		}  
		/**
		 * 设置字体大小
		 * @param tv
		 */
		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);    
		}
		/**
		 * 设置文本字体
		 * @param tv
		 */
		private void setTypefaceSpan(TextView tv) {
			SpannableString spanString = new SpannableString("文本字体textview");
			spanString.setSpan(new TypefaceSpan("monospace"), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			tv.append(spanString);
		}
		/**
		 * 添加URL超链接
		 * @param tv
		 */
		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());
		}  
		/**
		 * 在文字中显示图片表情等
		 * @param tv
		 */
		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);  
		} 
		/**
		 * 点击后跳转Activity
		 * @param textView
		 * @param packageContext
		 * @param cls
		 */
		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);
				}
				// 表示点击整个text的长度都有效触发这个事件
			}, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			textView.setText(spannableString);
			textView.setMovementMethod(LinkMovementMethod.getInstance());
		}
		/**
		 * 添加下划线
		 * @param tv
		 */
		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);  
		} 
		/**
		 * 添加删除线样式
		 * @param tv
		 */
		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);  
		}  


效果截图:

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


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

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

你可能感兴趣的:(android中TextView的SpannableString小结)