在我的 strings.xml 文件中,我定义了以下内容:
<string name="mystring"><b>Bold text</b>Non-bold text</string>
它应该可以工作,因为它指定了 here .但实际上只显示了粗体文本,其他部分的文本没有了。
最佳答案
取自this所以线程
使用这个
textView.setText(Html.fromHtml(someText));
或者通过这种方式从xml中获取
您可以在 strings.xml 中包含原始 HTML,只要您将其包装在
<![CDATA[ ...raw html... ]]>
例子:
<string name="nice_html">
<![CDATA[
<p>This is a html-formatted string with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph of the same string.</p>
]]>
</string>
然后,在您的代码中:
TextView foo = (TextView)findViewById(R.id.foo);
foo.setText(Html.fromHtml(getString(R.string.nice_html)));
您还可以在 CDATA block 内使用反斜杠转义撇号/单引号,这样您就可以使用类似 <b>can\'t</b>
的内容而不是无限丑陋的<b>can't</b>
关于Android:文本的格式化部分不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10785413/