XML within an Android string resource?

两盒软妹~` 提交于 2019-11-29 02:49:08

Yes you can, just use CDATA

<string name="stringName1"><![CDATA[<html>bla</html>]]></string>

It will obviously not work unless you escape characters in there such as < or > or &.

If you do encode the XML, it should work fine but probably not the best way to do it. I would prefer binary resource.


For putting in string.xml, you may encode using

String encoded = URLEncoder.encode(xml);

And decoding is the opposite.

For binary, you place it in RAW folder and you get a binary stream and turn it to string and load.

I have done this way:

Put your string in strings.xml

<string name="my_string"><![CDATA[Your long text here]]></string>

How to use:

<TextView
    android:id="@+id/textView"
    android:text="@string/my_string"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!