在开发中有时候需要给同一个TextView设置不同样式,比如修改某些字体颜色、字体大小、粗体、斜体等,再比如在文字中加一些图标等;
在Android中是支持Html标签的使用的,但并不是所有的HTML标签都支持。
HTML基础使用案例:
String content = "" + name + "";
if (Build.VERSION.SDK_INT >= 24){
text.setText(Html.fromHtml(content,Html.FROM_HTML_MODE_COMPACT));
}else{
text.setText(Html.fromHtml(content));
}
HTML图片文字混合使用案例:
String content = "";
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;
};
}
Android富文本
基础使用样式:
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:不包括开始,不包括结束
SpannableString spannableString = new SpannableString("xxxxxx");
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.GREEN);
spannableString.setSpan(foregroundColorSpan, 3,6,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
SpannableString spannableString = new SpannableString("xxxxxx");
BackgroudColorSpan backgroudColorSpan = new ForegroundColorSpan(Color.GREEN);
spannableString.setSpan(backgroudColorSpan, 3,6,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
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);
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);
SpannableString spannableString = new SpannableString("xxxxxx");
RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(1.5f);
spannableString.setSpan(relativeSizeSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
SpannableString spannableString = new SpannableString("xxxxxx");
AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(30, true);
spannableString.setSpan(absoluteSizeSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
SpannableString spannableString = new SpannableString("xxxxxx");
ScaleXSpan scaleXSpan= new ScaleXSpan(1.5f);
spannableString.setSpan(scaleXSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
//粗体
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
//斜体
StyleSpan italicSpan = new StyleSpan(Typeface.ITALIC);
//粗体+斜体
StyleSpan boldItalicSpan = new StyleSpan(Typeface.BOLD_ITALIC);
TypefaceSpan monospace = new TypefaceSpan("monospace");
TypefaceSpan serif = new TypefaceSpan("serif");
TypefaceSpan sans_serif = new TypefaceSpan("sans-serif");
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(this, android.R.style.TextAppearance_Material);
SpannableString spannableString = new SpannableString("xxxxxx");
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableString.setSpan(underlineSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
SpannableString spannableString = new SpannableString("xxxxxx");
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableString.setSpan(strikethroughSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTv.setText(spannableString);
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);
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);
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);