zai*_*aid 292 xml android 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
Run Code Online (Sandbox Code Playgroud)
Jos*_*ger 452
Android Asset Packaging Tool(aapt)在其最新版本中变得非常严格,现在用于所有 Android版本.生成的aapt-error是因为它不再允许非位置格式说明符而生成的.
以下是一些如何在资源字符串中包含%-symbol的想法.
如果您的字符串中不需要任何格式说明符或替换,则只需使用该formatted属性并将其设置为false:
<string formatted="false">%a + %a == 2%a</string>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,字符串不会用作格式字符串,Formatter因此您不必转义%-symbols.结果字符串是"%a +%a == 2%a".
如果省略该formatted="false"属性,则该字符串将用作格式字符串,您必须转义%-symbols.这是用double-%正确完成的:
<string>%%a + %%a == 2%%a</string>
Run Code Online (Sandbox Code Playgroud)
现在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"
Run Code Online (Sandbox Code Playgroud)
没有任何xml和代码,很难说出你的问题到底是什么,但希望这有助于你更好地理解机制.
Pau*_* E. 154
要允许应用程序使用来自资源的格式化字符串,您应该更正xml.所以,例如
<string name="app_name">Your App name, ver.%d</string>
Run Code Online (Sandbox Code Playgroud)
应该换成
<string name="app_name">Your App name, ver.%1$d</string>
Run Code Online (Sandbox Code Playgroud)
你可以看到这个细节.
小智 59
您可以使用%%转义%for XML解析器,但它在设备中显示两次.
要显示一次,请尝试以下格式: \%%
例如
<string name="zone_50">Fat Burning (50\%% to 60\%%)</string>
Run Code Online (Sandbox Code Playgroud)
如Fat Burning (50% to 60%)设备中所示
Vil*_*usK 35
使用
<string name="win_percentage">%d%% wins</string>
要得到
80% wins 作为格式化的字符串.
我正在使用String.format()方法来插入数字而不是%d.
Asa*_*ssi 10
在strings.xml文件中,您可以使用任何所需的Unicode符号.
例如,百分号的Unicode编号为0025:
<string name="percent_sign">%</string>
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看完整的Unicode标志列表
lan*_*ung 10
要逃避百分比符号,你只需要%%
例如 :
String.format("%1$d%%", 10)
Run Code Online (Sandbox Code Playgroud)
返回"10%"
不完全是你的问题,但类似的问题.
如果您的字符串条目中有多个格式,则不应多次使用"%s".
别 :
<string name="entry">Planned time %s - %s (%s)</string>
Run Code Online (Sandbox Code Playgroud)
做:
<string name="entry">Planned time %1$s - %2$s (%3$s)</string>
Run Code Online (Sandbox Code Playgroud)
一个棘手的方法:使用小百分号如下
<string name="zone_50">Fat Burning (50﹪ to 60﹪)</string>
Run Code Online (Sandbox Code Playgroud)
这可能是IDE变得过于严格的情况.
这个想法很合理,一般来说你应该指定替换变量的顺序,这样如果你为另一种语言添加资源,你的java代码就不需要改变了.但是这有两个问题:
首先,字符串如:
You will need %.5G %s
Run Code Online (Sandbox Code Playgroud)
使用,你将需要2.1200毫克将使任何语言的顺序相同,因为质量总是科学地按顺序表示.
第二个是如果你把变量的顺序放在你的默认资源(例如英语)的任何语言中,那么你只需要在语言的资源字符串中指定使用与默认语言不同的顺序的位置.
好消息是这很容易解决.尽管不需要指定位置,并且IDE过于严格,但无论如何都要指定它们.对于上面的例子,使用:
你将需要%1 $ .5G%2 $ s
| 归档时间: |
|
| 查看次数: |
110149 次 |
| 最近记录: |