For Android SDK, no resource defined in the res directory will define a dimension 1 Resource ID. During compilation, R. java is generated under the gen directory. The resource ID format is R. [type]. [id]. The resource type is resource type, such as anim, array, and attr.
You can access these resources in code or XML resource files (such as layout or style.
The Context class provides a method to access Resources. First, you can directly access Resources through getText, getString, and so on. In addition, the general method is to replace the Resources object through getResources, access Resources through the Resources object. Class inheritance graph for Context:
We can see that Application, Service, and Activity are all child classes of Context, so you can directly access resources in these classes:
[Java]
// Using the getString () conevenience method,
// Retrieve a string
// Resource that hapepns to have style information.
// Note the use
// CharSequence instead of String so we don't
// Lose the style info.
Cs = getText (R. string. styled_text );
TV = (TextView) findViewById (R. id. styled_text );
TV. setText (cs );
// Use the same resource, but convert it to
// String, which causes it
// To lose the style information.
Str = getString (R. string. styled_text );
TV = (TextView) findViewById (R. id. plain_text );
TV. setText (str );
// You might need to do this if your code
// Is not in an activity.
// For example View has a protected mContext
// Field you can use.
// In this case it's just 'this' since
// Activity is a context.
Context context = this;
// Get the Resources object from our context
Resources res = context. getResources ();
// Get the string resource, like above.
Cs = res. getText (R. string. styled_text );
TV = (TextView) findViewById (R. id. res1 );
TV. setText (cs );
// Using the getString () conevenience method,
// Retrieve a string
// Resource that hapepns to have style information.
// Note the use
// CharSequence instead of String so we don't
// Lose the style info.
Cs = getText (R. string. styled_text );
TV = (TextView) findViewById (R. id. styled_text );
TV. setText (cs );
// Use the same resource, but convert it to
// String, which causes it
// To lose the style information.
Str = getString (R. string. styled_text );
TV = (TextView) findViewById (R. id. plain_text );
TV. setText (str );
// You might need to do this if your code
// Is not in an activity.
// For example View has a protected mContext
// Field you can use.
// In this case it's just 'this' since
// Activity is a context.
Context context = this;
// Get the Resources object from our context
Resources res = context. getResources ();
// Get the string resource, like above.
Cs = res. getText (R. string. styled_text );
TV = (TextView) findViewById (R. id. res1 );
TV. setText (cs );
Author: mapdigit