关于动态替换字符串资源,在网上看到的一部分写法都不好用,就自己实验了一下
举例:
<string name="hello_world">确定花费%1$.1f元购买\n \"%2$s\"商品%3$d个吗?</string>
动态替换:
String data = getString(R.string.hello_world, 45.2f, "10kg大米", 500);
结果是:
确定花费45.2元购买
"10kg大米"商品500个吗?
其实就是下面这个方法
public final String getString (int resId,Object... formatArgs)
Added in
API level 1
Return a localized formatted string from the application's package's default string table, substituting the format arguments as defined inFormatter
and format(String, Object...)
.
Parameters
resId | Resource id for the format string |
---|---|
formatArgs | The format arguments that will be used for substitution. |