在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);