在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);
本文介绍了在Android开发中如何管理和使用字符串资源。详细说明了string.xml文件的作用及其与R文件的关系,并提供了从不同环境下获取字符串资源的方法。
1336

被折叠的 条评论
为什么被折叠?



