android中String.xml可以传参数的资源

在国际化程序的过程中我们需要使用String.xml文件进行字段替换,

下面是用来传递参数的写法

比如“我的名字叫李四,我来自首都北京”;这里的“李四”和“首都北京”都需要替换。

在string.xml中可以这样写,<string name="alert">我的名字叫%1$s,我来自%2$s</string>

在程序中:

String sAgeFormat1 = getResources().getString(R.string.alert); 
String sFinal1 = String.format(sAgeFormat1, "李四","首都北京");

这里两个string需要替换的,按照上面程序的顺序依次对应。

转载:http://hi.baidu.com/folk113/blog/item/d562f6d5f18d76143bf3cf18.html

原文地址:https://www.cnblogs.com/yourancao520/p/2537589.html