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

 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

当前回答

试试这个(右):

<string name="content" formatted="false">Great application %s  ☞  %s  ☞  %s \\n\\nGo to download this application %s</string>

其他回答

Use

<字符串=“win_percentage”>%d% wins</字符串>

得到

80%作为格式化字符串获胜。

我使用String.format()方法来获取插入的数字,而不是%d。

这可能是IDE变得过于严格的情况。

这个想法是合理的,一般来说,你应该指定替换变量的顺序,这样当你为另一种语言添加资源时,你的java代码就不需要改变了。然而,这里有两个问题:

首先,一个字符串,如:

You will need %.5G %s

你将需要2.1200毫克,在任何语言中顺序都是一样的,因为质量总量总是以科学的顺序表示。

第二,如果你把变量的顺序放在你默认资源指定的语言中(例如英语),那么你只需要指定资源字符串中的位置,使用与默认语言不同的顺序。

好消息是,这个问题很容易解决。即使不需要指定位置,IDE也过于严格,但无论如何都要指定它们。对于上面的例子使用:

您将需要%1$。5克% 2 $ s

在你的strings.xml文件中,你可以使用任何你想要的Unicode符号。

例如,百分比符号的Unicode数字是0025:

<string name="percent_sign">&#x0025;</string>

您可以在这里看到Unicode符号的综合列表

试试这个(右):

<string name="content" formatted="false">Great application %s  ☞  %s  ☞  %s \\n\\nGo to download this application %s</string>

对于XML解析器,可以使用%%转义%,但是在设备中会显示两次。

要显示一次,请尝试以下格式:\%%

例如

<string name="zone_50">Fat Burning (50\%% to 60\%%)</string> 

显示为 燃烧脂肪(50% - 60%)