在开发中有时候需要给同一个TextView设置不同样式,比如修改某些字体颜色、字体大小、粗体、斜体等,再比如在文字中加一些图标等;
1.Html.fromHtml
在Android中是支持Html标签的使用的,但并不是所有的HTML标签都支持。
HTML基础使用案例:
java
String content = "<font color='#FF0000'>" + name + "</font>";
if (Build.VERSION.SDK_INT >= 24){
text.setText(Html.fromHtml(content,Html.FROM_HTML_MODE_COMPACT));
}else{
text.setText(Html.fromHtml(content));
}
HTML图片文字混合使用案例:
java
String content = "<img src='" + getDrawableId(context, "icon_test") + "'>";
tv.setText(Html.fromHtml(content, getImageGetter(), null));
@SuppressLint("UseCompatLoadingForDrawables")
private Html.ImageGetter getImageGetter() {
return source -> {
Drawable drawable = mContext.getDrawable(Integer.parseInt(source));
drawable.setBounds(0, 0, 60, 60);
return drawable;
};
}
2.SpannableString
Android富文本
基础使用样式:
java
SpannableString spannableString = new SpannableString("xxxxxxxx");
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED);
spannableString.setSpan(backgroundColorSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
spannableString.setSpan(Object what, int start, int end, int flags);
参数详解:
- what:各种Span,可以对spannableString不同修饰
- ForegroundColorSpan:前景色
- BackgroundColorSpan:背景色
- ClickableSpan:抽象类,可点击效果,重写onClick方法响应点击事件
- URLSpan:超链接
- MaskFilterSpan:EmbossMaskFilter浮雕效果,BlurMaskFilter模糊效果
- RelativeSpan:文字相对大小
- AbsoluteSpan:文字绝对大小
- ScaleXSpan:x轴缩放
- styleSpan:文字样式
- TypefaceSpan:文字字体类型
- TextApearanceSpan:文字外貌
- UnderlineSpan:下划线
- StrikeThroughSpan:删除线
- SuperscriptSpan:上标
- SubscriptSpan:下标
- ImageSpan:图片
- int:文字开始的下标;
- end:文字结束的下标;
- flags:决定开始和结束下标是否包含下标
- SPAN_INCLUSIVE_EXCLUSIVE:包括开始,不包括结束
- SPAN_EXCLUSIVE_INCLUSIVE:不包括开始,包括结束
- SPAN_INCLUSIVE_INCLUSIVE:包括开始,包括结束
- SPAN_EXCLUSIVE_EXCLUSIVE:不包括开始,不包括结束
各种Span
ForegroundColorSpan 修改前景色 -- 文字颜色
java
SpannableString spannableString = new SpannableString("xxxxxx");
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.GREEN);
spannableString.setSpan(foregroundColorSpan, 3,6,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
BackgroudColorSpan 修改背景色
java
SpannableString spannableString = new SpannableString("xxxxxx");
BackgroudColorSpan backgroudColorSpan = new ForegroundColorSpan(Color.GREEN);
spannableString.setSpan(backgroudColorSpan, 3,6,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
ClickableSpan 设置文字点击事件
java
SpannableString spannableString = new SpannableString("xxx可点击");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "点击可点击按钮", Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
};
spannableString.setSpan(clickableSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setMovementMethod(LinkMovementMethod.getInstance());
mTv.setText(spannableString);
URLSpan 超链接 (继承于ClickableSpan,点击实现跳转到浏览器)
java
SpannableString spannableString = new SpannableString("xxxxxxx");
URLSpan urlSpan = new URLSpan("https://www.baidu.com");
spannableString.setSpan(urlSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setMovementMethod(LinkMovementMethod.getInstance());
mTv.setText(spannableString);
RelativeSizeSpan 字体的相对大小
javascript
SpannableString spannableString = new SpannableString("xxxxxx");
RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(1.5f);
spannableString.setSpan(relativeSizeSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
AbsoluteSizeSpan 字体的绝对大小 (true表示单位为dip,若为false则表示px)
java
SpannableString spannableString = new SpannableString("xxxxxx");
AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(30, true);
spannableString.setSpan(absoluteSizeSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
ScaleXSpan字体x轴缩放 (缩放倍数)
java
SpannableString spannableString = new SpannableString("xxxxxx");
ScaleXSpan scaleXSpan= new ScaleXSpan(1.5f);
spannableString.setSpan(scaleXSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
StyleSpan 文字样式,如斜体、粗体
java
//粗体
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
//斜体
StyleSpan italicSpan = new StyleSpan(Typeface.ITALIC);
//粗体+斜体
StyleSpan boldItalicSpan = new StyleSpan(Typeface.BOLD_ITALIC);
TypefaceSpan 文字字体类型,如monospace、serif和sans-serif等
java
TypefaceSpan monospace = new TypefaceSpan("monospace");
TypefaceSpan serif = new TypefaceSpan("serif");
TypefaceSpan sans_serif = new TypefaceSpan("sans-serif");
TextAppearanceSpan 文字外貌,通过style资源设置
java
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(this, android.R.style.TextAppearance_Material);
UnderlineSpan 文字下划线
java
SpannableString spannableString = new SpannableString("xxxxxx");
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableString.setSpan(underlineSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
StrikethroughSpan 文字删除线
java
SpannableString spannableString = new SpannableString("xxxxxx");
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableString.setSpan(strikethroughSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
SuperscriptSpan 文字上标
java
SpannableString spannableString = new SpannableString("xxxxxx");
SuperscriptSpan superscriptSpan = new SuperscriptSpan();
RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(0.8f);
spannableString.setSpan(relativeSizeSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(superscriptSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
SubscriptSpan 文字下标
java
SpannableString spannableString = new SpannableString("xxxxxx");
SubscriptSpan subscriptSpan = new SubscriptSpan();
RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(0.8f);
spannableString.setSpan(relativeSizeSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(subscriptSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
ImageSpan 图片
java
SpannableString spannableString = new SpannableString("xxxxxxx");
ImageSpan imageSpan = new ImageSpan(this, R.drawable.ic_test);
spannableString.setSpan(imageSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);