在开发中有时候需要给同一个TextView设置不同样式,比如修改某些字体颜色、字体大小、粗体、斜体等,再比如在文字中加一些图标等;

1.Html.fromHtml

在Android中是支持Html标签使用的,但并不是所有的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));
     }

HTML图片文字混合使用案例

    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文本

基础使用样式:

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);

参数详解:

各种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);
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);

原文地址:https://blog.csdn.net/weixin_42277946/article/details/134639963

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_8901.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注