android 各种 span的生成 如:修改TextView中部分文字的颜色 ,生成图片span

		textView = (TextView) findViewById(R.id.textview);
		SpannableStringBuilder builder = new SpannableStringBuilder(textView.getText().toString());
		
		//ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色
		ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
		ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE);
		ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE);
		ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN);
		ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW);
		


		builder.setSpan(redSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		builder.setSpan(whiteSpan, 1, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
		builder.setSpan(blueSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		builder.setSpan(greenSpan, 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		builder.setSpan(yellowSpan, 4,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		
		textView.setText(builder);


 

android 各种 span的生成 如:修改TextView中部分文字的颜色 ,生成图片span_第1张图片

 2) BackgroundColorSpan 文本背景色

private void setBackgroundColorSpan() {  
    SpannableString spanString = new SpannableString("背景色");  
    BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);  
    spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
    tv.append(spanString);  
} 

3) StyleSpan         字体样式:粗体、斜体等

private void setStyleSpan() {  
    SpannableString spanString = new SpannableString("粗体斜体");  
    StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);  
    spanString.setSpan(span, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
    tv.append(spanString);  
}  


 

 4)RelativeSizeSpan  相对大小

private void setRelativeFontSpan() {
	SpannableString spanString = new SpannableString("字体相对大小");
	spanString.setSpan(new RelativeSizeSpan(2.5f), 0, 6,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
	tv.append(spanString);    
}


 

5) TypefaceSpan         文本字体

private void setTypefaceSpan() {
	SpannableString spanString = new SpannableString("文本字体");
	spanString.setSpan(new TypefaceSpan("monospace"), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
	tv.append(spanText);
}

6) URLSpan 文本超链接

private void addUrlSpan() {  
    SpannableString spanString = new SpannableString("超链接");  
    URLSpan span = new URLSpan("http://www.baidu.com");  
    spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
    tv.append(spanString);  
}  


7) ImageSpan         图片

private void addImageSpan() {  
    SpannableString spanString = new SpannableString("3 ");  
    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);  
} 

 注意new SpannableString后面 添加了"3空格"3是的占位符号随意,  空格一定要有的,不然回到导致下面BUG:

1.连续输入多次imageSpan的时候:在换行的时候,会导致上面的一行ImageSpan消失

下面是我自己使用的方法,发现这个Bitmap转换成ImageSpan,比较精确的高度
	/**
	 * 将文字 生成 文字图片 生成显示编码的Bitmap,目前这个方法是可用的
	 * 
	 * @param contents
	 * @param context
	 * @return
	 */
	public static Bitmap creatCodeBitmap(String contents ,Context context) {
		float scale=context.getResources().getDisplayMetrics().scaledDensity;
		
		TextView tv = new TextView(context);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		tv.setLayoutParams(layoutParams);
		tv.setText(contents);
		tv.setTextSize(scale*12);
		tv.setGravity(Gravity.CENTER_HORIZONTAL);
		tv.setDrawingCacheEnabled(true);
		tv.setTextColor(Color.BLACK);
		tv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
				MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());


		tv.setBackgroundColor(Color.GREEN);
		
		tv.buildDrawingCache();
		Bitmap bitmapCode = tv.getDrawingCache();
		return bitmapCode;
	}

Android中 Bitmap和Drawable相互转换的方法 


8) ClickableSpan                 文本有点击事件

private TextView textView;
textView = (TextView)this.findViewById(R.id.textView);
String text = "显示Activity";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(new ClickableSpan() {
	@Override
	public void onClick(View widget) {
		Intent intent = new Intent(Main.this,OtherActivity.class);
		startActivity(intent);
	}
	// 表示点击整个text的长度都有效触发这个事件
}, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());

9) UnderlineSpan         下划线

private void addUnderLineSpan() {  
    SpannableString spanString = new SpannableString("下划线");  
    UnderlineSpan span = new UnderlineSpan();  
    spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
    tv.append(spanString);  
} 

10) StrikethroughSpan  删除线

private void addStrikeSpan() {  
    SpannableString spanString = new SpannableString("删除线");  
    StrikethroughSpan span = new StrikethroughSpan();  
    spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
    tv.append(spanString);  
}  

 

11) SuggestionSpan  相当于占位符
 


12) MaskFilterSpan  修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
 


13) RasterizerSpan   光栅效果
 


14) AbsoluteSizeSpan   绝对大小(文本字体)

private void setAbsoluteFontSpan() {
		SpannableString spannableString = new SpannableString("40号字体");
		AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(40);
		spannableString.setSpan(absoluteSizeSpan, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		editText.append(spannableString);
}








15) DynamicDrawableSpan    设置图片,基于文本基线或底部对齐。

 

16) TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)

private void setTextAppearanceSpan() {
	SpannableString spanString = new SpannableString("文本外貌");
	TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium);
	spanString.setSpan(textAppearanceSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
	tv.append(spanString);
}


 android 各种 span的生成 如:修改TextView中部分文字的颜色 ,生成图片span_第2张图片

http://blog.csdn.net/llping2011/article/details/10301291#

 

你可能感兴趣的:(EditText,ImageView)