3.App Resources-Accessing Resources

1. Accessing Resources

  Once you provide a resource in your application, you can apply it by referencing its resource ID. All resource IDs are defined in your

    project's R class, which the aapt tool automatically generates. 

  When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory. For

    each type of resource, there is an R subclass (for example, R.drawable for all drawable resources), and for each resource of that type,

    there is a static integer (for example, R.drawable.icon). This integer is the resource ID that you can use to retrieve your resource.

  Although the R class is where resource IDs are specified, you should never need to look there to discover a resource ID.

  There are two ways you can access a resource:

  <1> In Code

    Using a static integer from a sub-class of your R class, such as:

     R.string.hello 

  <2> In XML

    Using a special XML syntax that also corresponds to the resource ID defined in your R class, such as:

     @string/hello 

  

2. Accessing Resources in Code

  You can use a resource in code by passing the resource ID as a method parameter. For example, you can set an ImageView to use the

    res/drawable/myimage.png resource using setImageResource():

   1 ImageView imageView = (ImageView) findViewById(R.id.myimageview); 2 imageView.setImageResource(R.drawable.myimage); 

  2.1 Syntax

    [<package_name>.]R.<Resource_type>.<resource_name>

    <package_name> is the name of the package in which the resource is located (not required when referencing resources from your

              own package).

    <Resource_type> is the R subclass for the resource type.

    <resource_name> is either the resource filename without the extension or the android:name attribute value in the XML element

              (for simple values).

  2.2 Use cases

    There are many methods that accept a resource ID parameter and you can retrieve resources using methods in Resources. You can

      get an instance of Resources with Context.getResources(). 

// Load a background for the current screen from a drawable resource
getWindow().setBackgroundDrawableResource(R.drawable.my_background_image) ;

// Set the Activity title by getting a string from the Resources object, because
//  this method requires a CharSequence rather than a resource ID
getWindow().setTitle(getResources().getText(R.string.main_title));

// Load a custom layout for the current screen
setContentView(R.layout.main_screen);

// Set a slide in animation by getting an Animation from the Resources object
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
        R.anim.hyperspace_in));

// Set the text on a TextView object using a resource ID
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello_message);

 

3. Accessing Resources from XML

  3.1 Syntax

    @[<package_name>:]<resource_type>/<resource_name>

  3.2 Use cases

    To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@),

      use a question-mark (?), and the resource type portion is optional. For instance:

    ?[<package_name>:][<resource_type>/]<resource_name>

  3.3 Accessing Platform Resources

    Android contains a number of standard resources, such as styles, themes, and layouts. To access these resource, qualify your

      resource reference with the android package name. For example, Android provides a layout resource you can use for list items   

      in a ListAdapter:

   setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myarray)); 

    In this example, simple_list_item_1 is a layout resource defined by the platform for items in a ListView. You can use this instead of

      creating your own layout for list items. 

posted @ 2014-11-02 14:04  Mirrorhanman  阅读(151)  评论(0编辑  收藏  举报