【问题标题】:android string.xml reading html tags problemandroid string.xml读取html标签问题
【发布时间】:2011-06-05 23:38:06
【问题描述】:

在 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 标签的完整字符串

【问题讨论】:

    标签: android string


    【解决方案1】:

    使用 XML CDATA

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

    getString() 会得到"&lt;b&gt;ABC&lt;/b&gt;"

    【讨论】:

    • 不,不是,这太复杂了:stackoverflow.com/a/18199543/89818
    • 我的正在剥离它。我正在使用以下代码:getResources().getText(R.string.codename)
    • 这对我不起作用。我在getResources().getString() 上收到一个异常,说我的R.string.mystring 不存在。我进行了清理和重建,R 不会构建。我删除了我的 &lt;string&gt; 定义和 R 现在构建。出于某种原因,编译器不喜欢我的&lt;string&gt; 定义。它和你的一模一样,只是&lt;b&gt;ABC&lt;/b&gt;被我自己的html替换了。
    • @MarcoW.:OP 想知道如何显示包括标签在内的完整字符串,而不是如何格式化文本,因此答案是正确的。
    • 为什么需要内部&lt;Data&gt;标签?我认为只使用 CDATA 就足够了
    【解决方案2】:

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

    然后,在检索时:

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

    这是 android 文档中规定的做法。 阅读此链接中标题为“使用 HTML 标记进行样式设置”的段落: http://developer.android.com/guide/topics/resources/string-resource.html

    【讨论】:

    • 当我使用它时,它会沿着标签打印数据,而不是 html 格式
    • @Sarwar Erfan:它会打印标签,但实际上并没有设置字符串的样式,是吗?
    【解决方案3】:

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

    1. “string”节点需要将“formatted”属性设置为false
    2. 存储的 html 页面需要包装在 CData 节点中。
    3. 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>
    

    【讨论】:

    • 好答案。只有一件事:你可以有撇号,你只需要用 \ 来转义它们:\' 我使用它们是因为我需要将 javascript 存储在 strings.xml 中。
    【解决方案4】:

    直接将字符串资源 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>
    

    Activity.java 文件中的代码:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      相关资源
      最近更新 更多