335

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

 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
4

14 回答 14

491

Android 资产打包工具 ( )在其最新版本aapt中变得非常严格,现在用于所有Android 版本。您收到的 aapt-error 是因为它不再允许使用非位置格式说明符而生成的。

以下是一些如何在资源字符串中包含 %-symbol 的想法。

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

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

在这种情况下,字符串不用作格式字符串,Formatter因此您不必转义 %-symbols。结果字符串是“%a + %a == 2%a”。

如果省略该formatted="false"属性,则该字符串将用作格式字符串,并且您必须转义 %-symbols。这是用双%正确完成的:

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

现在aapt不会给您任何错误,但取决于您如何使用它,如果在Formatter没有任何格式参数的情况下调用 a ,则生成的字符串可以是 "%%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 和代码,很难说出您的问题到底是什么,但希望这可以帮助您更好地理解这些机制。

于 2010-12-11T15:06:44.977 回答
161

要允许应用程序使用来自资源的格式化字符串,您应该更正您的 xml。所以,例如

<string name="app_name">Your App name, ver.%d</string>

应该替换为

<string name="app_name">Your App name, ver.%1$d</string>

您可以查看内容以了解详细信息。

于 2011-02-25T06:44:23.147 回答
105

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

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

例如

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

显示为 Fat Burning (50% to 60%)设备

于 2011-10-07T17:46:59.450 回答
50

利用

<string name="win_percentage">%d%% wins</string>

要得到

80% wins作为格式化字符串。

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

于 2015-03-31T16:22:56.747 回答
16

要转义百分号,您只需要 %%

例如 :

String.format("%1$d%%", 10)

返回“10%”

于 2016-04-11T14:13:41.293 回答
14

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

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

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

您可以在此处查看Unicode 符号的完整列表

于 2013-04-29T07:24:58.240 回答
7

您可以使用 %% 转义 xml 中的 %,但您需要在代码中设置文本,而不是在布局 xml 中。

于 2011-03-15T08:54:36.250 回答
7

不完全是您的问题,而是类似的问题。

如果您的字符串条目中有多个格式,则不应多次使用“%s”。

不 :

<string name="entry">Planned time %s - %s (%s)</string>

做 :

<string name="entry">Planned time %1$s - %2$s (%3$s)</string>
于 2017-01-03T08:39:18.187 回答
6

一个棘手的方法:使用小百分号如下

 <string name="zone_50">Fat Burning (50&#65130; to 60&#65130;)</string> 

维基百科上的百分号

于 2015-05-29T07:40:53.057 回答
5

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

这个想法是合理的,通常您应该指定替换变量的顺序,这样如果您为另一种语言添加资源,您的 java 代码就不需要更改。但是,这样做有两个问题:

首先,一个字符串,例如:

You will need %.5G %s

要用作您将需要 2.1200 毫克将在任何语言中具有相同的顺序,因为该质量总是以科学的顺序表示。

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

好消息是这很容易解决。即使不需要指定位置,而且 IDE 过于严格,但还是要指定它们。对于上面的示例,请使用:

您将需要 %1$.5G %2$s

于 2014-07-28T22:14:24.037 回答
3

试试这个(右):

<string name="content" formatted="false">Great application %s  ☞  %s  ☞  %s \\n\\nGo to download this application %s</string>
于 2013-01-04T17:51:00.637 回答
1

根据谷歌官方文档,使用 %1$s 和 %2$s http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

你好,%1$s!您有 %2$d 条新消息。

于 2016-03-11T00:58:21.310 回答
-2

假设您要显示 (50% OFF) 并在运行时输入 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);
于 2017-02-07T11:21:16.057 回答
-19

尝试在它前面使用反斜杠,如下所示:

\%
于 2010-12-11T00:19:02.883 回答