我有一个字符串数组,其中使用了 %
符号。使用 %
的正确格式是 %
。当我在该数组中有一个包含多个 %
的字符串时,它会给我这个错误。
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 Assets 打包工具 (aapt
) 已变为 very strict in its latest release现在用于所有 Android 版本。您收到的 aapt-error 已生成,因为它不再允许 non-positional format specifiers .
以下是一些如何在资源字符串中包含 %-symbol 的想法。
如果您的字符串中不需要任何格式说明符或替换,您只需使用 formatted
属性并将其设置为 false
:
<string formatted="false">%a + %a == 2%a</string>
在这种情况下,字符串不用作 Formatter
的格式字符串。所以你不必逃避你的 %-symbols。结果字符串是“%a + %a == 2%a”。
如果您省略 formatted="false"
属性,则该字符串将用作格式字符串,并且您必须转义 %-symbols。使用 double-% 可以正确完成:
<string>%%a + %%a == 2%%a</string>
现在 aapt
不会给您任何错误,但取决于您使用它的方式,如果 Formatter
,结果字符串可能是 "%%a + %%a == 2%%a"调用时没有任何格式参数:
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 和代码,很难说出您的问题到底是什么,但希望这可以帮助您更好地理解机制。
关于Android XML 百分比符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4414389/