Android TextView Span属性
2019-10-21 本文已影响0人
陽光亽活力
- BackgroundColorSpan:给部分文字设置背景颜色
- ForegroundColorSpan:给部分文字设置前景色
- ClickableSpan:设置点击事件
- URLSpan:设置链接,相当于Html的标签
- MaskFilterSpan:文字的装饰效果。分为两种:BlurMaskFilter(模糊效果) 和 EmbossMaskFilter (浮雕效果)
- AbsoluteSizeSpan:设置字体大小
- RelativeSizeSpan:设置字体的相对大小
- ImageSpan:设置图片
- ScaleXSpan:横向压缩
- SubscriptSpan:设置下标脚注
- SuperscriptSpan:上标,相当于数学中的平方样式
- TextAppearanceSpan:使用style来定义文本样式
- TypefaceSpan:设置字体
- RasterizerSpan:设置光栅字样
- StrikethroughSpan:删除线,相当于购物网站上的划掉的原价
- UnderlineSpan:下划线。
public class MainActivity extends AppCompatActivity {
TextView tvTextViewSpan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTextViewSpan = findViewById(R.id.tv_text_view);
String textString = "这是一段文字,这个文字有背景,这个文字有前景,这个文字链接,模糊效果模糊,浮雕效果浮雕,这个文字放大,这个文字相对放大,这个文字设置图片,这个文字中设置上标下标,这个文字设置样式,这个文字设置字体,这个文字设置删除线,这个文字设置下划线";
SpannableString spannableString = new SpannableString(textString);
//设置文字背景
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(getResources().getColor(R.color.red));
spannableString.setSpan(backgroundColorSpan, 7, 15, SpannableString.SPAN_COMPOSING);
//设置文字颜色
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.GREEN);
spannableString.setSpan(foregroundColorSpan, 15, 23, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//设置文字链接
URLSpan urlSpan = new URLSpan("https://www.baidu.com");
spannableString.setSpan(urlSpan, 23, 29, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//浮雕效果
MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[]{1, 1, 3}, 1.5f, 8, 3));
spannableString.setSpan(maskFilterSpan, 29, 35, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//模糊效果
MaskFilter filter2 = new BlurMaskFilter(10, BlurMaskFilter.Blur.OUTER);
MaskFilterSpan maskFilterSpan2 = new MaskFilterSpan(filter2);
spannableString.setSpan(maskFilterSpan2, 36, 41, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//设置字体大小
AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(100);
spannableString.setSpan(absoluteSizeSpan, 43, 50, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//设置字体相对大小
RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(3);
spannableString.setSpan(relativeSizeSpan, 50, 60, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//设置图片
ImageSpan imageSpan = new ImageSpan(this, R.mipmap.ic1);
spannableString.setSpan(imageSpan, 59, 60, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//下标脚注
SubscriptSpan subscriptSpan = new SubscriptSpan();
spannableString.setSpan(subscriptSpan, 69, 70, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//上标
SuperscriptSpan superscriptSpan = new SuperscriptSpan();
spannableString.setSpan(superscriptSpan, 76, 77, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//定义样式
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(this,R.style.text_16_D8);
spannableString.setSpan(textAppearanceSpan, 81, 89, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//定义字体
TypefaceSpan typefaceSpan = new TypefaceSpan("monospace");
spannableString.setSpan(typefaceSpan, 89, 98, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//添加删除线
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableString.setSpan(strikethroughSpan, 99, 108, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//添加下划线
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableString.setSpan(underlineSpan, 109, 118, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvTextViewSpan.setText(spannableString);
}
image.png