2017-10-18 46 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標籤解決? –

    相關問題