Android Context Role

Source: Internet
Author: User

Context interface for accessing global information

App Resources: strings, drawable resources, etc.

Let's look at a particle that uses context to scope resources

 Public classMainactivity extends Activity {String TAG="Carloz"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); //Activity itself is a ContextTextView TV =NewTextView (mainactivity. This); Tv.settext ("Hello Carloz"); Tv.settext (R.string. Hello_world);                Setcontentview (TV); String Str= (String) getapplicationcontext (). Getresources (). GetText (R.string. Hello_world);        LOG.D (TAG, str); //output 08-16 11:59:27.402:d/carloz (13255): Hello world!ImageView IV=NewImageView (mainactivity. This);        Iv.setimageresource (R.drawable.ic_launcher);    Setcontentview (iv); }}

From the above code can be seen to create a new TextView or ImageView at least one parameter context,activity itself is a Context, so you can copy.

Tv.settext (R.string.hello_world);  A string ID is passed in, and the code inside the SetText API is GetContext (). Getresources (). GetText (Resid); As you can see, getting resources requires a context.

Android Context Role

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.