Android的XML百分号 [英] Android XML Percent Symbol

查看:426
本文介绍了Android的XML百分号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中的符号用于字符串数组。正确格式为使用&放大器;#37; 。当我有一个字符串中有多个数组&放大器;#37; 它给了我这个错误

 在此找到多个注解
 线:
  - 错误:非定位格式指定多重替换;
   您是不是要增加格式化=false属性?
  - 错误:发现标签< /项目>其中,< /字符串数组>是期待
 

解决方案

Android的资产打包工具( AAPT )已经成为的很严,在其最新发布,现在用于的所有的安卓版本。生成的AAPT错误你得到,因为它不再允许<一href="http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling">non-positional格式说明的。

下面是一些建议,如何可以在您的资源字符串%-symbol。

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

 &LT;字符串格式化=假&GT;%A +%一== 2%&LT; /串&GT;
 

在这种情况下,字符串是不是用作格式字符串 格式化 所以你不必来逃避%-symbols。结果字符串为%A +%一== 2%。

如果您省略了格式=假属性,将字符串作为格式字符串,你必须转义%-symbols。这是正确的双完成 - %:

 &LT;字符串&GT; %% A + %%一个== 2 %% A&LT; /串&GT;
 

现在 AAPT 给你任何错误,但取决于你如何使用它,结果字符串可以是%% A + %%一个== 2 %%一个如果 格式化是没有任何调用 格式的参数的:

 资源资源= context.getResources();

字符串S1 = res.getString(R.string.str);
// S1 ==%% A + %%一个== 2 %%一个

字符串s2 = res.getString(R.string.str,NULL);
// S2 ==%A +%一== 2%的
 

在没有任何XML和code这是很难说究竟你的问题,但希望这可以帮助您了解的机制更好一点。

I have an array of strings in which the % symbol is used. Proper format for using the % is &#37;. When I have a string in that array with multiple &#37; it gives me this error.

 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

解决方案

The Android Asset Packaging Tool (aapt) has become very strict in its latest release and is now used for all Android versions. The aapt-error you're getting is generated because it no longer allows non-positional format specifiers.

Here are a few ideas how you can include the %-symbol in your resource strings.

If you don't need any format specifiers or substitutions in your string you can simply make use of the formatted attribute and set it to false:

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

In this case the string is not used as a format string for the Formatter so you don't have to escape your %-symbols. The resulting string is "%a + %a == 2%a".

If you omit the formatted="false" attribute, the string is used as a format string and you have to escape the %-symbols. This is correctly done with double-%:

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

Now aapt gives you no errors but depending on how you use it, the resulting string can be "%%a + %%a == 2%%a" if a Formatter is invoked without any format arguments:

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"

Without any xml and code it is difficult to say what exactly your problem is but hopefully this helps you understand the mechanisms a little better.

这篇关于Android的XML百分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆