android string.xml reading html tags problem

前端 未结 4 1361
栀梦
栀梦 2020-11-29 03:44

In Android project\'s strings.xml file i have following html text







        
相关标签:
4条回答
  • 2020-11-29 04:15

    i've run into the same problem by trying to store a complete htmlpage in my rescources. I finaly solved the problem by changing three things:

    1. the "string" node needs to have set the "formatted" attribute to false.
    2. the stored html page needs to be wrapped in a CData node.
    3. the html page is NOT alowed to contain apostrophes!

    The last one actualy was my main problem. So here is my strings.xml containing the "properly" stored html page.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="error_html" formatted="false" ><![CDATA[<html><head><link name="icon1" href="favicon.ico" rel="SHORTCUT ICON" /><title>Error</title><style>html, body {margin: 0;padding: 0;background: #3f0000;color: white;font-family: Arial;}#MainLink {position: relative;background: #7f0000;margin: 10px;text-decoration: none;border: 1px solid #9f0000;-webkit-border-radius: 10px;-moz-border-radius: 10px;border-radius: 10px;-webkit-box-shadow: 5px 5px 5px 0px rgba(0,0,0,0.5);-moz-box-shadow: 5px 5px 5px 0px rgba(0,0,0,0.5);box-shadow: 5px 5px 5px 0px rgba(0,0,0,0.5);}#MainLink {width: 462px;height: 220px;}#MainLink td {font-size: 20px;}#MainLink span {text-decoration: underline;font-weight: bold;font-size: 40px;}</style></head><body><table width="100%" height="100%" cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle"><table cellpadding="0" cellspacing="0"><tr><td colspan="2" id="MainLink" align="center"><big><big><b>Error</b></big></big></td></tr></table></td></tr></table></body></html>]]></string>
    </resources>
    
    0 讨论(0)
  • 2020-11-29 04:27

    Replace < with &lt;

    <string name="myHeadStr">&lt;b>&lt;u>bold, underline &lt;/u>&lt;/b></string>
    

    Then, when retrieving:

    Html.fromHtml(getResources().getString(R.string.myHeadStr));
    

    This is the prescribed way of doing in android documentation. Read the paragraph titled: "Styling with HTML markup" in this link: http://developer.android.com/guide/topics/resources/string-resource.html

    0 讨论(0)
  • 2020-11-29 04:33

    Use XML CDATA

    <string name="demoStr"><Data><![CDATA[ <b>ABC</b> ]]> </Data></string>
    

    getString() will be got "<b>ABC</b>"

    0 讨论(0)
  • 2020-11-29 04:38

    Directly passing the string resource id to setText() or using Context.getText() without Html.fromHtml() works properly but passing in the the result of Context.getString() does not.

    ex:

    strings.xml:

    <resources>
        <string name="html">This is <b>bold</b> and this is <i>italic</i>.</string>
    <resources>
    

    code in Activity.java file:

    textView.setText(R.string.html); // this will properly format the text
    textView.setText(getText(R.string.html)); // this will properly format the text
    textView.setText(getString(R.string.html)); // this will ignore the formatting tags
    
    0 讨论(0)
提交回复
热议问题