在开发中有时候需要给同一个TextView设置不同样式,比如修改某些字体颜色、字体大小、粗体、斜体等,再比如在文字中加一些图标等;
1.Html.fromHtml
在Android中是支持Html标签的使用的,但并不是所有的HTML标签都支持。
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));
}
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
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 修改前景色 — 文字颜色
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 文字样式,如斜体、粗体
//粗体
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);
-
UnderlineSpan 文字下划线
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);
-
SubscriptSpan 文字下标
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 图片
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);
原文地址:https://blog.csdn.net/weixin_42277946/article/details/134639963
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_8901.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。