I know I can put escaped HTML tags in string resources. However, looking at the source code for the Contacts application I can see that they have a way of not having to encode the HTML. Quote from the Contacts application strings.xml:
<string name="contactsSyncPlug"><font fgcolor="#ffffffff">Sync your Google contacts!</font>
\nAfter syncing to your phone, your contacts will be available to you wherever you go.</string>
Unfortunately, when I try something similar (like Hello, <b>World</b>!
), getString()
returns the string without the tags (I can see that in logcat
). Why is that? How can I get the original string, with tags and everything? How is the Contacts application doing it?
ベストアンサー1
You can also surround your html in a CDATA
block as well and getString()
will return your actual HTML. Like such:
<string name="foo"><![CDATA[Foo Bar <a href="foo?id=%s">baz</a> is cool]]></string>
Now when you perform a getString(R.string.foo)
the string will be HTML. If you need to render the HTML (with the link as shown) via a clickable TextView
you'd need to perform a Html.fromHtml(...)
call to get the spannable text.