的方式来支持<跨度>在TextView的标签 [英] Way to support <span> tag in TextView

查看:118
本文介绍了的方式来支持<跨度>在TextView的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想亮点我的文字的某部分将在的TextView 显示。我知道,我们可以使用 SpannableString 在Android中,突出我的文字的某部分。但我想知道,我们如何使用<跨度> HTML标签做同样的事情。我试图达到这一点,用code如下:

 字符串str =<跨度风格= \背景颜色:#FFFF00 \>突出显示和LT; / SPAN>文字;
TextView的电视=(TextView中)findViewById(R.id.textView);
tv.setText(Html.fromHtml(STR));
 

但显示的文字是没有范围标记相同的文字。

什么是错的?我该怎么做同样的事情,而无需使用 SpannableString

在此先感谢:)

解决方案
  

什么是错的?

Html.fromHtml()在什么标签,它支持条款的限制。 下面是某一个博客帖子几年前,呈现出标签名册 fromHtml()支持即可。即使是在<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/Html.java"相对=nofollow>目前的code (截至发稿时),没有生成 BackgroundColorSpan ,你正在寻找。

没有什么从创建一个更丰富的HTML停止某人 - > 跨区转换器,它可以处理这样的&LT;跨度&GT; 标签,虽然我不知道有任何存在于present时间。

因此​​,此刻,你会被卡住应用 BackgroundColorSpan 自己。 下面是一个示例项目,我用 BackgroundColorSpan 来突出显示搜索字符串的特定子串的结果。

I want to highlight some part of my text that will be shown in TextView. I know that we can use SpannableString in Android to highlight some part of my text. But I want to know, how can we use <span> html tag to do same thing. I tried to reach this, using code as following :

String str = "<span style=\"background-color:#ffff00\"> Highlighted </span> Text";
TextView tv = (TextView) findViewById(R.id.textView);
tv.setText(Html.fromHtml(str));

But shown text is the same text without span tag.

What is wrong ? How can I do same thing without using SpannableString ?

Thanks in advance :)

解决方案

What is wrong ?

Html.fromHtml() is limited in terms of what tags it supports. Here is a blog post from a couple of years ago showing the roster of tags fromHtml() supported then. Even in the current code (as of the time of this writing), nothing generates the BackgroundColorSpan that you are seeking.

There is nothing stopping somebody from creating a richer HTML->Spanned converter that could handle such <span> tags, though I am not aware of any existing at the present time.

Hence, at the moment, you will be stuck applying a BackgroundColorSpan yourself. Here is a sample project where I use BackgroundColorSpan to highlight the results of searching a string for particular sub-strings.

这篇关于的方式来支持&LT;跨度&GT;在TextView的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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