Android资源文件中出现百分号

在进行系统定制工作时遇到适配其它语言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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值