android获取string.xml的值

 

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

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

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

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

 
  1. android:text="@string/resource_name"  

2.在activity里: 

 
  1. 方法一:this.getString(R.string.resource_name);  
  2. 方法二:getResources().getString(R.string.resource_name);  

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

  1. context.getString(R.string.resource_name);  
  2. application.getString(R.string.resource_name);  
原文地址:https://www.cnblogs.com/yelanggu/p/10750800.html