Html.fromHtml()无法使用Text的背景色 [英] Html.fromHtml() not working with background color of Text

查看:90
本文介绍了Html.fromHtml()无法使用Text的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Html.fromHtml() TextView 中设置背景.特别是,我想在第一个单词上设置背景.

I am trying to set a background in a TextView using Html.fromHtml(). In particulare, I want to set the background on the first word.

我使用了以下代码:

Html.fromHtml("<font color='red'>("+someText+")</font>");

,它以文本颜色成功执行.但是我想更改背景颜色.

and it is executing successfully with text color. However I want to change the background color.

我该怎么做?

推荐答案

尝试一下:

TextView TV = (TextView) findViewById(R.id.mytextview01);

Spannable wordtoSpan = new SpannableString("hello hi. how are you?");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new BackgroundColorSpan(Color.RED), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(wordtoSpan);

这是用于设置文本和背景颜色(后者使用 BackgroundColorSpan ).

This is to set both text and background color (the latter with BackgroundColorSpan).

这篇关于Html.fromHtml()无法使用Text的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆