Android获取string.xml的值

来源:互联网 发布:2016淘宝严查刷单小号 编辑:程序博客网 时间:2024/06/05 06:31

在android开发过程中,编写java代码中的常量过一般情况下,我们是定义在string.xml这个文件中。这样修改起来也很方便,而且做国际化也很简单。

这个string.xml的值会被R文件映射,所以可以看到R文件全是定义为int类型,就像是一个地址指引一样。

获取string.xml文件里面的值有几个不同的地方。

1. 在AndroidManifest.xml与layout等xml文件里:

android:text="@string/resource_name"  


2.在activity里:

方法一:this.getString(R.string.resource_name); 

方法二:getResources().getString(R.string.resource_name);  


3在其他java文件(必须有Context或pplication)

context.getString(R.string.resource_name); 

application.getString(R.string.resource_name);