转载

Android -- SpannableString

SpannableString

Android通过SpannableString类来对EditText和TextView的指定文本进行处理。

ForegroundColorSpan

文本颜色

private void setForegroundColorSpan() {         SpannableString spanString = new SpannableString("文本颜色");         ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);         spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);         tv.append(spanString);     }   

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);     }   

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);     } 

RelativeSizeSpan

相对大小

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

TypefaceSpan

文本字体

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

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);     }   

ImageSpan

图片

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

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());   

UnderlineSpan

下划线

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

StrikethroughSpan

删除线

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

SuggestionSpan

占位符

MaskFilterSpan

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

RasterizerSpan

光栅效果

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);   }  

DynamicDrawableSpan

设置图片(基于文本基线或底部对齐)

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);   }  

我是天王盖地虎的分割线

正文到此结束
Loading...