How to make Resources.getStringArray work with HTM

2019-08-31 12:08发布

I have an XML file in res/values with a string array, whose items contains simple HTML markup, such as:

<resources>
    <string-array name="frases31">
        <item>They <u>were not</u> before.</item>
        <item>I don\'t think <u>he</u> is.</item>
...

I load these into a String[] array this way:

Resources res = mContext.getResources();
exercise.setSentences(res.getStringArray(R.array.frases31));

The trouble is the String[] array in variable exercise, which is called sentences contains only the strings, the HTML markup (<u> and </u>) has disappeared. Should I store the result of calling res.getStringArray(R.array.frases31) in a special variable? I have an index and I will put sentences[i] in a TextView programmatically.

1条回答
贼婆χ
2楼-- · 2019-08-31 12:31

You'll need to edit the XML to achieve this. You can wrap your items with CDATA to preserve the markup:

<item><![CDATA[They <u>were not</u> before.]]></item>
<item><![CDATA[I don\'t think <u>he</u> is.]]></item>

Or escape each markup character individually:

<item>They &lt;u&gt;were not&lt;/u&gt; before.</item>
<item>I don\'t think &lt;u&gt;he&lt;/u&gt; is.</item>
查看更多
登录 后发表回答