Android SpannableString属性详解

  • 1、BackgroundColorSpan 背景色
  • 2、ClickableSpan 文本可点击,有点击事件
  • 3、ForegroundColorSpan 文本颜色(前景色)
  • 4、MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
  • 5、MetricAffectingSpan 父类,一般不用
  • 6、RasterizerSpan 光栅效果
  • 7、StrikethroughSpan 删除线(中划线)
  • 8、SuggestionSpan 相当于占位符
  • 9、UnderlineSpan 下划线
  • 10、AbsoluteSizeSpan 绝对大小(文本字体)
  • 11、DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。
  • 12、ImageSpan 图片
  • 13、RelativeSizeSpan 相对大小(文本字体)
  • 14、ReplacementSpan 父类,一般不用
  • 15、ScaleXSpan 基于x轴缩放
  • 16、StyleSpan 字体样式:粗体、斜体等
  • 17、SubscriptSpan 下标(数学公式会用到)
  • 18、SuperscriptSpan 上标(数学公式会用到)
  • 19、TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)
  • 20、TypefaceSpan 文本字体
  • 21、URLSpan 文本超链接

BackgroundColorSpan 背景色

1
2
3
4
SpannableString spanText = new SpannableString("BackgroundColorSpan");
spanText.setSpan(new BackgroundColorSpan(Color.GREEN), 0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

ForegroundColorSpan 前景色

文本颜色(前景色)

1
2
3
4
SpannableString spanText = new SpannableString("ForegroundColorSpan");
spanText.setSpan(new ForegroundColorSpan(Color.BLUE), 6, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

MaskFilterSpan 修饰效果

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

1
2
3
4
5
6
7
8
9
10
SpannableString spanText = new SpannableString("MaskFilterSpan");
int length = spanText.length();
//模糊(BlurMaskFilter)
MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(3, Blur.OUTER));
spanText.setSpan(maskFilterSpan, 0, length - 10, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//浮雕(EmbossMaskFilter)
maskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[]{1,1,3}, 1.5f, 8, 3));
spanText.setSpan(maskFilterSpan, length - 10, length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

RasterizerSpan 光栅效果

1
2
3
4
SpannableString spanText = new SpannableString("RasterizerSpan");
spanText.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

StrikethroughSpan 删除线

1
2
3
4
SpannableString spanText = new SpannableString("StrikethroughSpan");
spanText.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

SuggestionSpan 占位符

相当于占位符,一般用在EditText输入框中。当双击此文本时,会弹出提示框选择一些建议(推荐的)文字,选中的文本将替换此占位符。在输入法上用的较多。

UnderlineSpan 下划线

1
2
3
4
SpannableString spanText = new SpannableString("UnderlineSpan");
spanText.setSpan(new UnderlineSpan(), 0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

AbsoluteSizeSpan 绝对大小(文本字体)

1
2
3
4
SpannableString spanText = new SpannableString("AbsoluteSizeSpan");
spanText.setSpan(new AbsoluteSizeSpan(20, true), 0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

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

设置图片,基于文本基线或底部对齐,左边图片基于基线对齐,右边图片基于底部对齐。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
DynamicDrawableSpan drawableSpan = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE) {
@Override
public Drawable getDrawable() {
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0, 50, 50);
return d;
}
};

DynamicDrawableSpan drawableSpan2 = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) {
@Override
public Drawable getDrawable() {
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0, 50, 50);
return d;
}
};

SpannableString spanText = new SpannableString("DynamicDrawableSpan");
spanText.setSpan(drawableSpan, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
spanText.setSpan(drawableSpan2, 7, 8, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

ImageSpan 图片

和DynamicDrawableSpan差别不大

1
2
3
4
5
6
SpannableString spanText = new SpannableString("ImageSpan");
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0, 50, 50);
spanText.setSpan(new ImageSpan(d), 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

RelativeSizeSpan 相对大小(文本字体)

相对大小的比例是基于当前文本字体大小

1
2
3
4
5
SpannableString spanText = new SpannableString("RelativeSizeSpan");
//参数proportion:比例大小
spanText.setSpan(new RelativeSizeSpan(2.5f), 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

ScaleXSpan 基于x轴缩放

1
2
3
4
5
SpannableString spanText = new SpannableString("ScaleXSpan");
//参数proportion:比例大小
spanText.setSpan(new ScaleXSpan(3.8f), 3, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

StyleSpan 字体样式

字体样式:粗体、斜体等

1
2
3
4
5
SpannableString spanText = new SpannableString("StyleSpan");
//Typeface.BOLD_ITALIC: 粗体+斜体
spanText.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 3, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

SubscriptSpan 下标(数学公式会用到)

1
2
3
4
SpannableString spanText = new SpannableString("SubscriptSpan");
spanText.setSpan(new SubscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

SuperscriptSpan 上标(数学公式会用到)

1
2
3
4
SpannableString spanText = new SpannableString("SuperscriptSpan");
spanText.setSpan(new SuperscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

TextAppearanceSpan 文本外貌

包括字体、大小、样式和颜色。系统还提供了相关值TextAppearance_Small, TextAppearance_Large等。如有需要可在以上样式基础上修改。

1
2
3
4
5
SpannableString spanText = new SpannableString("TextAppearanceSpan");
//若需自定义TextAppearance,可以在系统样式上进行修改
spanText.setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

TypefaceSpan 文本字体

1
2
3
4
5
6
SpannableString spanText = new SpannableString("TypefaceSpan");
//若需使用自定义字体,可能要重写类TypefaceSpan
spanText.setSpan(new TypefaceSpan("monospace"), 3, 10,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);

URLSpan 文本超链接

1
2
3
4
5
6
7
SpannableString spanText = new SpannableString("URLSpan");
spanText.setSpan(new URLSpan("http://www.androidios.cc"), 10, spanText.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.append("\n");
mTextView.append(spanText);
//让URLSpan可以点击
mTextView.setMovementMethod(new LinkMovementMethod());

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :