java - Android:格式化字符串并添加粗体样式

标签 java android string format

我的资源中有一个字符串:

<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 with fromHtml(String), after the formatting takes place.

在任何 xml 资源文件中插入时,您需要对 html 标记的左括号进行 HTML 转义。

<string name="region">&lt;b>Region:&lt;/b> %1$s</string>

关于java - Android:格式化字符串并添加粗体样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25201602/

相关文章:

javascript - Regex(正则表达式),替换javascript中出现的第二个

java - 升级到 Spring Security 4 后尝试登录时出现 404

Java 错误;需要数组,但找到了 java.lang.String

android - 如何将参数传递给 View 模型?

android - 依赖 org.apache.httpcomponents :httpclient:4. 4.1 被忽略发布因为它可能与 Android 提供的内部版本冲突

android - performTraversals 和 onConfigurationChanged 中的竞争条件导致布局不正确?

c# - 空字符串上的 ToString

java - 在 Java String.replaceAll() 正则表达式中使用函数或方法

Java 正则表达式删除新行,但保留空格。

java - Intent .CATEGORY_APP_CALCULATOR : ActivityNotFoundException