Android开发中的那些坑之-------------string.xml里面的通配符

来源:互联网 发布:苹果手机备份软件 编辑:程序博客网 时间:2024/05/16 17:16

今天来说一下string.xml里面的通配符的坑

1,在xml里面我们通常用通配符代表一些不确定内容,例如:

<string name="bank_add_unsupport_xcard">不支持绑定%s信用卡,及其他银行信用卡(如扫描有误请手动输入银行卡)</string>
这是没有任何毛病的,但是当出现多个通配符的时候,你如果这样写,

<string name="bank_change_unsupport_xcard">不支持绑定%s信用卡,请绑定%s的借记卡(如扫描有误请手动输入银行卡)</string>
那么毛病就出现了,会出现如下错误:

Error:(514) Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?

Error:(514) Unexpected end tag string

Error:(514) Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?

Error:(514) Unexpected end tag string

没错出现上面的问题,就是因为多个通配符的时候,他没法一一对应,需要我们手动支出

这个时候,我们就需要指出每个通配符的位置,解决方法如下:

<string name="bank_change_unsupport_xcard">不支持绑定%1$s信用卡,请绑定%2$s的借记卡(如扫描有误请手动输入银行卡)</string>

2,顺便科普一下通配符

%n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格 
%n$md:代表输出的是整数,n代表是第几个参数,设置m的值可以在输出之前放置空格,也可以设为0m,在输出之前放置m个0 
%n$mf:代表输出的是浮点数,n代表是第几个参数,设置m的值可以控制小数位数,如m=2.2时,输出格式为00.00 

也可简单写成:

%d   (表示整数)

%f    (表示浮点数)

%s   (表示字符串)



原创粉丝点击