android获取string.xml的值 .

 

在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或application)

context.getString(R.string.resource_name);
application.getString(R.string.resource_name);

 

 

posted @ 2013-05-15 11:28  那片风景  阅读(167)  评论(0)    收藏  举报