【问题标题】:Android string-array display Percent SignAndroid 字符串数组显示百分号
【发布时间】:2011-03-28 21:34:49
【问题描述】:

我的 strings.xml 文件中有一个字符串数组,其中包含多个项目。只有其中一项具有多个需要显示的百分比“%”符号。似乎根本没有关于如何逃脱它的建议。 \%, %% 或在字符串数组或项目标签上使用 formatted="false"。当我使用 \% Eclipses 显示项目错误时,如果我使用 %% 则 Eclipse 运行良好,但文本显示两个百分号“%%”。那么关于如何在 strings.xml 中转义 % 的任何想法?

<string-array name="details">
     <item>Placement of things</item>
     <item>Percentage of times placed is 30% 
         and percentage of times release 50%</item>
</string-array>

【问题讨论】:

  • 显然在字符串数组中的同一字符串项中转义多个百分比存在错误。我可以使用 %一次,如果我在同一个项目中多次使用它,它就会出错。
  • 奇怪,我可以完美地添加“%”,没问题。你能准确解释一下你的代码是做什么的吗?
  • 没什么好解释的 我想逐字显示这两个字符串。它的 Eclipse 给了我错误,我无法编译。

标签: android arrays xml


【解决方案1】:

您的 ADT 插件和 SDK 更新了吗? 我的已更新,没有显示此错误。只需 % 即可发挥魅力。

无论如何,您是否尝试将字符串放入 CDATA 中?

<string-array name="details">
     <item>Placement of things</item>
     <item><![CDATA[Percentage of times placed is 30% 
             and percentage of times release 50%]]></item>
</string-array>

【讨论】:

  • 嗯,我得到了错误,我很确定我有最新的更新。似乎很奇怪,我尝试了 CDATA 方法,但它在 Eclipse 中也出错了。
【解决方案2】:

显然使用旧版本的 eclipse 并且没有最新的 Android SDK 会产生这个问题。我将eclipse升级到3.6.2并确保安装最新版本的android sdk r10。所以希望这可以帮助某人并节省他们的时间,因为我浪费了几个小时试图修复/找到答案。

因此,这个问题的答案是,只要您拥有最新版本的 SDK 和 Eclipse,您就可以在字符串数组项中多次使用单个 % 符号而无需对其进行转义、加倍或使用 CDATA .

【讨论】:

  • 有趣的答案,即使正确有负面评价......不知道为什么有人会不同意,因为这是解决方案。
【解决方案3】:

我有同样的问题,但无事可做,在 JB 4.1.2 上对我没有任何帮助。
这是我尝试过的:

<string-array name="percentage">
    <item>a. 100%</item>            <!-- unknownFormatConversionException -->
    <item>b. 90&#37;</item>         <!-- unknownFormatConversionException -->
    <item>c. 80%%</item>            <!-- "somewhere" a double %% appears -->
    <item>d. 60\%</item>            <!-- no % appears -->
    <item>e. 40\%%</item>           <!-- unknownFormatConversionException -->
    <item>f. 30%\%</item>           <!-- unknownFormatConversionException -->
    <item><![CDATA[g. 20%]]></item> <!-- unknownFormatConversionException -->
    <item formatter="false>h. 10%</item>    <!-- unknownFormatConversionException -->
</string-array>

使用仿真器,我已经看到在 ICS 4.0 之前一切正常,就像在我的 Galaxy-S GB 2.3.5 中一样,而在使用 ICS 及更高版本时,会出现 unknownFormatConversionExceptions,就像在我的 Nexus S JB 4.1.2 上一样。
最后,this 是我的最终决定:完全省略百分比符号!

<item>a. 100</item>
<item>b. 90</item>
...

这只是一个非常粗略的解决方法,但至少我可以继续工作!

我在 Ubuntu 13.04 下使用 ADT Build: v22.0.1-685705
测试设备:Nexus S JB 4.1.2、三星 Galaxy S GB 2.3.5

【讨论】:

  • 哇,这仍然是更新的 android 版本的问题吗?好吧,它是三星,他们做事不同。
【解决方案4】:

一个棘手的方法是使用百分号的 unicode 字符

U+FE6A ﹪ small percent sign (HTML &#65130; )
U+FF05 % full-width percent sign (HTML &#65285;)

在 XML 中你可以定义如下

    <string-array name="details">
         <item>Placement of things</item>
         <item>Percentage of times placed is 30&#65130; 
             and percentage of times release 50&#65130;</item>
    </string-array>

Percent Sign on Wikipedia

【讨论】:

    【解决方案5】:

    对我有用的是在 strings.xml 中声明一个带有百分比符号和属性的新字符串 formatted="false"

    <string name="percents" formatted="false">10&#37; is not 20&#37;</string
    

    然后在arrays.xml中

    <string-array name="my_array">
        <item>@string/percents</item>
        ...
    

    【讨论】:

      猜你喜欢
      • 2011-10-21
      • 1970-01-01
      • 2013-05-02
      • 2015-11-17
      • 2019-05-15
      • 2015-11-05
      • 2022-01-20
      • 2021-06-18
      • 1970-01-01
      相关资源
      最近更新 更多