Android ApiDemos example resolution (48): Content-& gt; Resources-& gt; R

Source: Internet
Author: User

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
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.