为什么HtmlCompat.fromHtml()没有设置文本样式? [英] Why HtmlCompat.fromHtml() is not styling the text?
问题描述
我正在尝试设置 String
的一部分的样式,然后再将其设置为 TextView
.但是当我运行该应用程序时,没有样式应用于文本.
I am trying to style a part of String
before setting it to TextView
. But when I run the application, no styling is applied to the text.
我在 strings.xml 中将字符串资源定义为
I have defined the string resource in strings.xml as
<string name="amount_added_notice"> <![CDATA[Amount of <b><font color=\'#333333\'>\u20B9%1$s</font></b> added to %2$s Account]]> </string>
在 XML
中,我正在使用 DataBinding
设置文本
In the XML
, I am using DataBinding
to set text
android:text="@{viewModel.firstText}"
firstText
变量在 ViewModel
中定义为
val firstText by lazy { ObservableField<String>() }
然后将 string
分配如下,
firstText.set( HtmlCompat.fromHtml(context.getString(R.string.amount_added_notice, "500", "John's"), FROM_HTML_MODE_LEGACY)))
我在做什么错了?
推荐答案
val firstText by lazy { ObservableField<String>() }
尝试将其更改为:
val firstText by lazy { ObservableField<CharSequence>() }
,看看是否有帮助. HtmlCompat.fromHtml()
的输出是 CharSequence
,特别是某种包含格式设置规则的 Spannable
.我的猜测是它正在下转换为普通的 String
.
and see if that helps. The output of HtmlCompat.fromHtml()
is a CharSequence
, specifically some sort of Spannable
that contains the formatting rules. My guess is that is is getting down-converted into a plain String
.
这篇关于为什么HtmlCompat.fromHtml()没有设置文本样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!