XML within an Android string resource?

后端 未结 3 1003
梦毁少年i
梦毁少年i 2020-12-16 09:19

I was wondering if I could place XML within /res/values/strings.xml? I ask this because I am checking for the XML data file for my application, if it does not e

相关标签:
3条回答
  • 2020-12-16 09:41

    Yes you can, just use CDATA

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

    0 讨论(0)
  • 2020-12-16 09:56

    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

    0 讨论(0)
  • 2020-12-16 09:57

    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.

    0 讨论(0)
提交回复
热议问题