SpannableString常用用法

1. 给文字设置前景色

 private void setFgColor(String text, TextView textView, int start, int end) {
 SpannableString spannableString = new SpannableString(text);
 ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#0099EE"));
 spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
 textView.setText(spannableString);
 }

2.给文字设置背景色

  private void setBgColor(String text, TextView textView, int start, int end) {
  SpannableString spannableString = new SpannableString(text);
  BackgroundColorSpan colorSpan = new BackgroundColorSpan(Color.parseColor("#0099EE"));
  spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  textView.setText(spannableString);
  }

3.给文字设置删除线

private void setDeleteLine(String text, TextView textView, int start, int end) {
SpannableString spannableString = new SpannableString(text);
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableString.setSpan(strikethroughSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}

4.给文字设置下划线

private void setUnderLine(String text, TextView textView, int start, int end) {
SpannableString spannableString = new SpannableString(text);
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableString.setSpan(underlineSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}

5.给文字设置上标

private void setSuperscript(String text, TextView textView, int start, int end) {
SpannableString spannableString = new SpannableString(text);
SuperscriptSpan superscriptSpan = new SuperscriptSpan();
spannableString.setSpan(superscriptSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}

6.给文字设置下标

private void setSubscript(String text, TextView textView, int start, int end) {
SpannableString spannableString = new SpannableString(text);
SubscriptSpan subscriptSpan = new SubscriptSpan();
spannableString.setSpan(subscriptSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}

7.为文字设置粗体、斜体风格

private void setSubscript(String text, TextView textView, int start, int end) {
SpannableString spannableString = new SpannableString(text);
StyleSpan styleSpan_B = new StyleSpan(Typeface.BOLD);
StyleSpan styleSpan_I = new StyleSpan(Typeface.ITALIC);
spannableString.setSpan(styleSpan_B, 5, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(styleSpan_I, 8, 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setHighlightColor(Color.parseColor("#36969696"));
textView.setText(spannableString);
}

8.在文本中添加表情(表情)

private void setSubscript(String text, TextView textView, int start, int end) {
SpannableString spannableString = new SpannableString(text);
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan imageSpan = new ImageSpan(drawable);
spannableString.setSpan(imageSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}

9.为文字设置超链接

private void setSubscript(String text, TextView textView, int start, int end) {
SpannableString spannableString = new SpannableString(text);
URLSpan urlSpan = new URLSpan("http://www.baidu.com");
spannableString.setSpan(urlSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.parseColor("#36969696"));
textView.setText(spannableString);
}

10.为文字设置点击事件

private void setSubscript(String text, TextView textView, int start, int end) {
SpannableString spannableString = new SpannableString(text);
MyClickableSpan clickableSpan = new MyClickableSpan("http://www.baidu.com");
spannableString.setSpan(clickableSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.parseColor("#36969696"));
textView.setText(spannableString);
}

/***************************************************************/

 class MyClickableSpan extends ClickableSpan {

 private String content;

  public MyClickableSpan(String content) {
      this.content = content;
  }

  @Override
  public void updateDrawState(TextPaint ds) {
      ds.setUnderlineText(false);
  }

  @Override
  public void onClick(View widget) {
      Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
  }
 }

你可能感兴趣的:(SpannableString常用用法)