在一个TextView富文本文本、TextView

2023-09-05 10:43:22 作者:久而旧之

我需要为我的程序中创建一个信息。我想填补信息文本的几种颜色,样式,字体,ECC。

I need create an "Info" for my program. I'd like fill the info text with several colors, style, fonts, ecc.

我发现这一点:http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring

有另一种方式(更容易)来做到这一点?

is there another way (better easy) to do this?

谢谢!

推荐答案

在阅读你的问题的标题,我ppared给你一个链接到具体的文章$ P $。我使用的跨度发现, SpannableString 是相当强大的。备用是使用HTML格式的文本,它可以与Html.fromHtml().

Upon reading your question title, I was prepared to give you a link to that exact article. I've found using spans and SpannableString to be quite powerful. The alternate would be to use HTML formatting for your text, which can be done with Html.fromHtml().

不过,这不完全明确支持哪些HTML标签,因为文件只读取:

However, It's not exactly clear which HTML tags are supported, since the documentation simply reads:

这个类处理HTML字符串转换成可显示的样式文本。不   所有的HTML标签的支持。

This class processes HTML strings into displayable styled text. Not all HTML tags are supported.

因此​​,对于一个很好的解决方案,我肯定会去的文章 SpannableString 办法。这种方法是非常方便的做法,因为你也可以让自己的跨度类,扩展现有的(例如,如果你想拥有一段文字使用不内置自定义字体)。

So, for a good solution, I would definitely go with the SpannableString approach in the article. This method is very handy in practice since you can also make your own span classes that extend the existing ones (for example, if you want to have a section of text use a custom font that isn't built in).