Use the String Resources
You can reference your string resources in your source code and other XML files using the resource name defined by the
<string>
element‘s name
attribute.
In your source code, you can refer to a string resource with the syntax R.string.<string_name>
. There are a variety of methods that accept a string resource this way.
For example:
// Get a string resource from your app‘sResources
String hello =getResources()
.getString(R.string.hello_world); // Or supply a string resource to a method that requires a stringTextView textView = new TextView(this);textView.setText(R.string.hello_world);
In other XML files, you can refer to a string resource with the syntax @string/<string_name>
whenever the XML attribute accepts a string value.
For example:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
时间: 2024-11-03 01:34:18