使用高亮文本Html.fromHtml [英] Highlighting text using Html.fromHtml

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

问题描述

我尝试添加背景颜色里面MultiAutoCompleteTextView字符串。

I am trying to add background color for the string inside MultiAutoCompleteTextView.

我重写在multipleAutocompletetextview replaceText方法,试图用这样的HTML代替charactersequence

I am overriding replaceText method in multipleAutocompletetextview, trying to replace charactersequence with html like this

@Override
protected void replaceText(CharSequence text) {

    // TODO Auto-generated method stub
    String styledText = "<font color='red'>"+text+"</font>.";
    super.replaceText(Html.fromHtml(styledText));
}

这是工作的罚款。我可以改变字体颜色。但我想添加背景颜色的字体。任何人都可以建议我,如何实现这一目标?

this is working fine. I Can change the font color. But i want to add background color for the font. Can anybody suggest me, how to achieve this?

(我们添加的标签,同时创造问题,samething我试图用MultiAutoCompleteTextView实现的方式。我想补充的背景所选字符串)。

( The way we are adding tags while creating question, samething i am trying to implement using MultiAutoCompleteTextView. I want to add background for the selected string. )

在此先感谢

推荐答案

Html.fromHtml 不支持设置背景颜色。

Html.fromHtml does not support setting the background color.

您将不得不采取通过Htm​​l.fromHtml返回的SpannedString并设置 BackgroundColorSpan 您要设置的背景颜色的文本。

You will either have to take the SpannedString returned by Html.fromHtml and set the BackgroundColorSpan to the text you want to set the background color of.

是这样的:

new SpannableString(styledText).setSpan(
    new BackgroundColorSpan( Color.YELLOW), 0, styledText.length(),
      Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

另请参阅:
http://developer.android.com/reference/android/text/SpannableString.html
http://developer.android.com/reference/android/text/style/package-summary.html

您可以同时通过自己的TagHandler到 Html.fromHtml(字符串源,Html.ImageGetter imageGetter,Html.TagHandler tagHandler)来支持更多的HTML标签。
请参见 http://developer.android.com/reference/android/text/ Html.TagHandler.html

You can as well pass your own TagHandler into Html.fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) to support additional HTML tags. See http://developer.android.com/reference/android/text/Html.TagHandler.html

这篇关于使用高亮文本Html.fromHtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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