Android XML 百分比符号

标签 android xml android-resources xliff

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

 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/

相关文章:

安卓.view.InflateException : Binary XML file line #7: Error inflating class fragment

c# - 使用 gps 定位系统服务依赖注入(inject)

android - 解析 Android 嵌套查询

Android 全屏布局,下方有额外的布局,可滚动

java - 获取字符串数组的值

android - 单击时 ListView

android - 如何修复 Android 开发中的 Error Parsing XML?

xml - SwiProlog中的xpath查询中的运算符(operator)预期错误

android - 根据android中的性别从string.xml资源文件加载

android.R.id.home 找不到符号