使stringArg中的formatArg变为粗体Andoid

问题描述 投票:0回答:1

我有格式参数为%s的字符串资源

<string name="string_name">Hello Mr. %s. I want to ...</string>

此字符串资源已本地化,因此长度因地区而异,必须在一个TextView中

我想制作formatArg %s粗体样式

我尝试过html标签<string name="string_name">Hello Mr. <b>%s</b>. I want to ...</string>

我尝试了注释标记<string name="string_name">Hello Mr. <annotation style="bold">%s</annotation>. I want to ...</string>

我创建了SpannableString并尝试将Span设置为该注释

并且在将文本设置为TextView之后,对字符串参数也没有任何影响

tvDescription.setText(getString(R.string.string_name, "formatArg"));

是否可以在格式化的字符串资源中为字符串参数设置样式?

android spannablestring formatted-text
1个回答
0
投票

这是我设置AlertDialog消息样式的方式:

Spanned htmlAsSpanned = Html.fromHtml("Some html tags are working in here<br /><br />
<b>this is bold text</b>");

编辑:或者也许您正在寻找this。他们的例子是这样的:

String text = getString(R.string.welcome_messages, "Roko Spark", 5);
Spanned styledText = Html.fromHtml(text);

textView.setText(styledText);

在此格式化的字符串中,添加了元素。请注意左括号用&lt;符号。

<string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
© www.soinside.com 2019 - 2024. All rights reserved.