首先创建一个 /values/strings.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="title">My Application</string> <string name="hello_world">Hello World!</string> </resources>
你可以参考你的源代码和其他XML字符串资源文件中使用的资源名称定义的<字符串>元素的name属性。
在你的源代码,您可以参考与语法R.string字符串资源。< string_name >。有各种各样的方法接受一个字符串资源。
// Get a string resource from your app's在其他XML文件,您可以参考语法的字符串资源@string / < string_name >每当XML属性接受一个字符串值。Resources
String hello =getResources()
.getString(R.string.hello_world); // Or supply a string resource to a method that requires a string TextView textView = new TextView(this); textView.setText(R.string.hello_world);
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />