Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android string.xml读取html标记问题_Android_String - Fatal编程技术网

android string.xml读取html标记问题

android string.xml读取html标记问题,android,string,Android,String,在Android项目的strings.xml文件中,我有以下html文本 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="myHeadStr"><b><u>bold, underline </u></b></string> ... </resources> 粗体,下划线 ... 当我把它读入get

在Android项目的strings.xml文件中,我有以下html文本

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="myHeadStr"><b><u>bold, underline </u></b></string>
...

</resources>

粗体,下划线
...
当我把它读入getString(R.string.myHeadStr)时,它只给出文本“粗体,下划线”,它忘记了html标记,并且

如何从string.xml读取带有html标记的完整字符串使用xml CDATA

<string name="demoStr"><Data><![CDATA[ <b>ABC</b> ]]> </Data></string>
ABC]]>
getString()将被“ABC”替换为

<string name="myHeadStr">&lt;b>&lt;u>bold, underline &lt;/u>&lt;/b></string>
这是android文档中规定的方法。 阅读此链接中标题为“使用HTML标记设置样式”的段落:

我在重新资源中存储完整的htmlpage时遇到了同样的问题。 我通过改变三件事最终解决了这个问题:

  • “string”节点需要将“formatted”属性设置为false
  • 存储的html页面需要包装在CData节点中
  • html页面不允许包含撇号 最后一个问题实际上是我的主要问题。 下面是我的strings.xml,其中包含“正确”存储的html页面

    <?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>
    
    
    Errorhtml,正文{边距:0;填充:0;背景:#3f0000;颜色:白色;字体系列:Arial;}主链接{位置:相对;背景:#7f0000;边距:10px;文本装饰:无;边框:1px实心#9f0000;-webkit边框半径:10px;-moz边框半径:10px;边框半径:10px;-webkit方框阴影:5px 5px 5px 0px rgba(0,0,0.5)-moz盒影:5px 5px 5px 0px rgba(0,0,0,0.5);盒影:5px 5px 5px 0px rgba(0,0,0,0.5)}主链接{宽度:462px;高度:220px;}主链接td{字体大小:20px;}主链接跨度{文本装饰:下划线;字体重量:粗体;字体大小:40px;}错误]>
    
    直接将字符串资源id传递给
    setText()
    或使用
    Context.getText()
    而不使用
    Html.fromHtml()
    可以正常工作,但传递
    Context.getString()的结果则不行

    例:

    strings.xml

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

    当我使用它时,它会像打印html一样沿着标记打印数据formatting@Sarwar埃尔凡:它打印标签,但实际上它并没有设计字符串的样式,是吗?不,它不是,这太复杂了:我的正在剥离它。我正在使用以下代码:
    getResources().getText(R.string.codename)
    这对我不起作用。我在
    getResources().getString()
    上收到一个异常,说我的
    R.string.mystring
    不存在。我做了一次清理和重建,但
    R
    无法构建。我删除了我的
    定义,现在构建了
    R
    。由于某些原因,编译器不喜欢我的
    定义。它与您的完全相同,只是
    ABC
    被替换为我自己的html。@MarcoW:OP想知道如何显示包含标记的完整字符串,而不是如何格式化文本,因此答案是正确的。为什么需要内部
    标记?我认为只使用CDATA是一个很好的答案。只有一件事:你可以有撇号,你只需要用\:\'来转义它们。我使用它们是因为我需要将javascript存储在strings.xml中。
    <resources>
        <string name="html">This is <b>bold</b> and this is <i>italic</i>.</string>
    <resources>
    
    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