如果动态的修改Android中strings.xml文件中的值,在这里给大家推荐一种简单的方法。
strings.xml中节点是支持占位符的,如下所示:
其中%后面是占位符的位置,从1开始
$ 后面是填充数据的类型:
%d:表示整数型;
%f :表示浮点型,其中f前面的.2表示小数的位数
%s:表示字符串
启动运行:
name:张三, age:23, 浮点数:1000000.0
来自:http://blog.csdn.net/coolszy/article/details/6579578
strings.xml中节点是支持占位符的,如下所示:
<string name="data">name:%1$s, age:%2$d, 浮点数:%3$.2f</string>
其中%后面是占位符的位置,从1开始
$ 后面是填充数据的类型:
%d:表示整数型;
%f :表示浮点型,其中f前面的.2表示小数的位数
%s:表示字符串
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView)findViewById(R.id.dataText);
String data = getResources().getString(R.string.data);
data = String.format(data, "张三",23,1000000.0);
view.setText(data);
}
启动运行:
name:张三, age:23, 浮点数:1000000.0
来自:http://blog.csdn.net/coolszy/article/details/6579578