Android中的一串strings.xml中的粗体字

我在strings.xml中的一个string中有一个长文本。 我想要大胆地改变文本中某些单词的颜色。

我该怎么做?

提前致谢

你基本上可以在你的string资源中使用html标签,如:

<resource> <string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string> </resources> 

并使用Html.fromHtml或使用spannable,检查我发布的链接。

旧的类似的问题: 是否有可能在一个TextView中有多个样式?

在string资源中使用html标记: –

 <resources> <string name="string_resource_name"><![CDATA[<b> Your text </b>]]> </string> </resources> 

从string资源获取粗体文本,如:

 private Spanned getSpannedText(String text) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT); } else { return Html.fromHtml(text); } } String s = format(context.getResources().getString(R.string.string_resource_name)); textView.setText(getSpannedText(s));