2017-10-18 49 views
0

我可以看到有多种方式可以使用Html字符串。在Android strings.xml中使用HTML字符串的最佳方式是什么?我在下面提到了两种方法:哪一种是可取的,为什么?在Android strings.xml中使用HTML字符串的最佳方式

  1. 使用CDATA

<string name="test"><![CDATA[Test Message.<br/><br/>Please contact <a href="mailto:[email protected]" target="_top">[email protected]</a> for more details.</string>

  • 使用Unicode
  • <string name="test">Test Message.&lt;br/&gt;&lt;br/&gt;Please contact &lt;a href="mailto:[email protected]" target="_top"&gt;[email protected]&lt;/a&gt; for more details.</string>

    回答

    0

    在你的情况,你可以这样做

    Spanned s = Html.fromHtml(getResources().getString(R.string.test)); 
    textView.setText(s); 
    

    和string.xml

    <string name="test"> 
        Test Message. 
        <br/> 
        <br/> 
        Please contact 
        <a href="mailto:[email protected]" target="_top">[email protected]</a> 
        for more details. 
    </string> 
    
    +0

    我不找任何具体情况。上面的字符串只是一个例子。我需要了解每种方法的优缺点。在少数设备上,'<'可能会被一些垃圾替代,所以最好使用unicode。这个问题是否也可以通过使用CDATA标签解决? –

    相关问题