在进行系统定制工作时遇到适配其它语言string.xml资源文件中包含% 出现的问题:
报错的代码提示:
packages/apps/Settings/res/values-pt/strings.xml:21: error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
packages/apps/Settings/res/values-pt/strings.xml:21: error: Unexpected end tag string
packages/apps/Settings/res/values-pt-rBR/strings.xml:21: error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
packages/apps/Settings/res/values-pt-rBR/strings.xml:21: error: Unexpected end tag string
packages/apps/Settings/res/values-pt-rPT/strings.xml:21: error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
packages/apps/Settings/res/values-pt-rPT/strings.xml:21: error: Unexpected end tag string
出现这个错误的原因主要是因为strings字串中包含百分号(%);
解决办法:
在string.xml 文件中添加 formatted=”false”, 参数根据需求来定,由于我是直接在布局文件中写的引用的值,并且该值不需要变化,所以添加这个参数;
<string name="tv_brightness_warning_message" msgid="1152390750360770793" formatted="false"> Valores de brilho maiores que 80% podem consumir mais bateria. É recomendável manter este valor em torno de 50% para aumentar o tempo de uso de seu aparelho.</string>
如果需要在代码中进行引用就需要变化
<string name="tv_brightness_warning_message" msgid="1152390750360770793" formatted="false"> Valores de brilho maiores que %1$s podem consumir mais bateria. É recomendável manter este valor em torno de %2$s para aumentar o tempo de uso de seu aparelho.</string>
就是将xml中需要变化的值进行动态的替换掉,80% 和50%;
在代码中实现:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), numbOne, numbTwo);