我有一个字符串数组,其中使用了%
符号。使用%
的正确格式为%
。当我在该数组中有一个包含多个%
的字符串时,它会给我这个错误。
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
答案 0 :(得分:438)
Android资产打包工具(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和代码,很难说出你的问题究竟是什么,但希望这有助于你更好地理解这些机制。
答案 1 :(得分:153)
要允许应用程序使用资源中的格式化字符串,您应该更正xml。所以,例如
<string name="app_name">Your App name, ver.%d</string>
应替换为
<string name="app_name">Your App name, ver.%1$d</string>
您可以查看this了解详情。
答案 2 :(得分:55)
您可以使用%%转义%for XML解析器,但它会在设备中显示两次。
要显示一次,请尝试以下格式:\%%
例如
<string name="zone_50">Fat Burning (50\%% to 60\%%)</string>
显示为
设备中的Fat Burning (50% to 60%)
答案 3 :(得分:29)
使用
<string name="win_percentage">%d%% wins</string>
获取
80% wins
作为格式化字符串。
我正在使用String.format()
方法来插入数字,而不是%d
。
答案 4 :(得分:10)
在strings.xml文件中,您可以使用任何所需的Unicode符号。
例如,百分号的Unicode编号为0025:
<string name="percent_sign">%</string>
您可以看到全面的Unicode标志列表here
答案 5 :(得分:7)
不完全是你的问题,而是类似的问题。
如果您的字符串条目中有多个格式,则不应多次使用“%s”。
请勿:
<string name="entry">Planned time %s - %s (%s)</string>
DO:
<string name="entry">Planned time %1$s - %2$s (%3$s)</string>
答案 6 :(得分:6)
要转义百分比符号,您只需要%%
例如:
String.format("%1$d%%", 10)
返回“10%”
答案 7 :(得分:5)
您可以使用%%转义xml中的%,但您需要在代码中设置文本,而不是在布局xml中设置。
答案 8 :(得分:5)
这可能是IDE过于严格的情况。
这个想法很合理,一般来说你应该指定替换变量的顺序,这样如果你为另一种语言添加资源,你的java代码就不需要改变了。但是有两个问题:
首先是一个字符串,如:
You will need %.5G %s
用作你需要2.1200毫克将使任何语言的顺序相同,因为质量总是以科学的顺序表示。
第二个是如果你把变量的顺序放在你所指定的默认资源的任何语言中(例如英语),那么你只需要在语言的资源字符串中指定位置,使用与默认值不同的顺序语言。
好消息是这很容易解决。尽管不需要指定位置,并且IDE过于严格,但无论如何都要指定它们。对于上面的示例,请使用:
您将需要%1 $ .5G%2 $ s
答案 9 :(得分:3)
试试这个(右):
<string name="content" formatted="false">Great application %s ☞ %s ☞ %s \\n\\nGo to download this application %s</string>
答案 10 :(得分:3)
一种棘手的方法:使用如下的小百分号
<string name="zone_50">Fat Burning (50﹪ to 60﹪)</string>
答案 11 :(得分:1)
根据Google官方文档,请使用%1 $ s和%2 $ s http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
你好,%1 $ s!您有%2 $ d条新消息。
答案 12 :(得分:-1)
假设您要显示(50%折扣)并在运行时输入50。这是代码:
<string name="format_discount"> (
<xliff:g id="discount">%1$s</xliff:g>
<xliff:g id="percentage_sign">%2$s</xliff:g>
OFF)</string>
在java类中使用以下代码:
String formattedString=String.format(context.getString(R.string.format_discount),discountString,"%");
holder1.mTextViewDiscount.setText(formattedString);
答案 13 :(得分:-16)
尝试在它前面使用反斜杠,如下所示:
\%