Android XML 百分比符号 [英] Android XML Percent Symbol

查看:27
本文介绍了Android XML 百分比符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组,其中使用了 % 符号.使用 % 的正确格式是 %.当我在该数组中有一个带有多个 % 的字符串时,它会给我这个错误.

I have an array of strings in which the % symbol is used. Proper format for using the % is %. When I have a string in that array with multiple % it gives me this error.

 Multiple annotations found at this
 line:
 - error: Multiple substitutions specified in non-positional format;
   did you mean to add the formatted="false" attribute?
 - error: Found tag </item> where </string-array> is expected

推荐答案

Android Asset Packaging Tool (aapt) 已成为 在其最新版本中非常严格,现在用于所有 Android 版本.您得到的 aapt 错误是因为它不再允许 非位置格式说明符.

The Android Asset Packaging Tool (aapt) has become very strict in its latest release and is now used for all Android versions. The aapt-error you're getting is generated because it no longer allows non-positional format specifiers.

这里有一些关于如何在资源字符串中包含 % 符号的想法.

Here are a few ideas how you can include the %-symbol in your resource strings.

如果您的字符串中不需要任何格式说明符或替换,您可以简单地使用 formatted 属性并将其设置为 false:

If you don't need any format specifiers or substitutions in your string you can simply make use of the formatted attribute and set it to false:

<string formatted="false">%a + %a == 2%a</string>

在这种情况下,该字符串不用作 Formatter 所以你不必逃避你的 %-symbols.结果字符串是%a + %a == 2%a".

In this case the string is not used as a format string for the Formatter so you don't have to escape your %-symbols. The resulting string is "%a + %a == 2%a".

如果您省略 formatted="false" 属性,则该字符串将用作格式字符串,您必须对 % 符号进行转义.这可以通过 double-% 正确完成:

If you omit the formatted="false" attribute, the string is used as a format string and you have to escape the %-symbols. This is correctly done with double-%:

<string>%%a + %%a == 2%%a</string>

现在 aapt 不会给你任何错误,但取决于你如何使用它,如果 Formatter 调用时没有任何格式参数:

Now aapt gives you no errors but depending on how you use it, the resulting string can be "%%a + %%a == 2%%a" if a Formatter is invoked without any format arguments:

Resources res = context.getResources();

String s1 = res.getString(R.string.str);
// s1 == "%%a + %%a == 2%%a"

String s2 = res.getString(R.string.str, null);
// s2 == "%a + %a == 2%a"

如果没有任何 xml 和代码,很难说出您的问题究竟是什么,但希望这可以帮助您更好地理解这些机制.

Without any xml and code it is difficult to say what exactly your problem is but hopefully this helps you understand the mechanisms a little better.

这篇关于Android XML 百分比符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆