我的资源中有一个字符串:
<string name="region"><b>Region:</b> %1$s</string>
我正在尝试将此文本设置为 TextView :
tvRegion.setText(Html.fromHtml(R.string.region, "France"));
问题是字符串“region”不是粗体!
最佳答案
来自 the official documentation :
Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the
String.format(String, Object...)
method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered withfromHtml(String)
, after the formatting takes place.
在任何 xml 资源文件中插入时,您需要对 html 标记的左括号进行 HTML 转义。
<string name="region"><b>Region:</b> %1$s</string>
关于java - Android:格式化字符串并添加粗体样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25201602/