R.java File Description

Source: Internet
Author: User

Http://blog.chinaunix.net/uid-21411227-id-4133828.html

Note: R.java The file cannot be modified manually.

1. r.java file parsing in HelloWorld project

Package Com.android.hellworld;

Public final class R {

public static final class Attr {

}

public static final class Drawable {

public static final int icon=0x7f020000;

}

public static final class layout {

public static final int main=0x7f030000;

}

public static final class string {

public static final int app_name=0x7f040001;

public static final int hello=0x7f040000;

}

}

The R.java file has a default of four static inner classes such as attr, drawable, Layout, string, and each static inner class corresponds to a resource, such as layout static inner class corresponding to the interface file in layout. Static constants in each of these static inner classes define a resource identifier, such as public static final int main=0x7f030000, and corresponding to the Main.xml file in the layout directory.

Since the current "drawable-*dpi" directory is only icon.png a picture file, so at this time different details of the same name of the Icon.png file in the drawable inner class always have only one icon property. If we add another image under the "drawable-*dpi" directory, an icon property is automatically added to the inner class.

Add a resource's naming convention: Resource files can only be initials with lowercase letters and underscores, and then only [a-z0-9_] can appear in the name. these characters, otherwise R.java files are not automatically updated, and Eclipse will prompt for errors.

Understand the source of the content in the R.java file, that is, when a developer adds the appropriate type of file to any subdirectory in the res/directory, ADT automatically generates a constant of type static int in the corresponding inner class in the R.java file, indexing the added file. If a new interface is added to the layout directory, the corresponding static int constant is added to the public static final class layout. Instead, when we delete any file in the Res directory, its corresponding record in R.java will be automatically deleted by ADT.

R.java In addition to the index function that automatically identifies the resource, there is another major feature, if the res A resource in the directory is not used in the app, and the system does not compile the corresponding resource into the app's apk when the app is compiled package, so you can save Android the resources of the phone.

2. How to use the R.java file to refer to the required resources

1 ) in Java referencing resources in programs

In the Java syntax to refer to: R.resource_type.resource_name

Note: Resource_name does not require a suffix name for the file

The Android system itself comes with a lot of resources, and we can refer to it just by adding "Android" to the front. To claim that the resource came from the Android system, namely: Android.r.resource_type.resouce_name

Example: Icon.png under "Drawable-*hpi"

The picture in the R.java file is mapped to the drawable inner class under the icon parameter, if you want to get the resource, the notation is: R.drawble.icon

2. Referencing Resources in an XML file

Format: @[package:]type/name

In XML files, such as Main.xml and androidmainfest.xml files, are obtained by means of "@drawable/icon". where "@" stands for the R.java class. "Drawable" represents the static inner class "drawable" in R.java, "/icon" represents the static property "icon" in the static inner class "drawable", which can point to "drawable-*dpi" under the Res directory The Icon.png resource in the. Other types of files are similar. All resources defined in the R file can be obtained by means of "@Static_inner_classes_name/resource_name". such as "@id/button", "@string/app_name".

If you are accessing a file that comes with your Android system, add the package name "Android:". Example: android:textcolor= "@android: color/red"

in the layout file, when we need to add the ID attribute as identity for some components, we can use the following expression:"@+id/string_name", where "+" means R.java, an internal class named ID, adds a record to China. Example: The meaning of "@+id/button" is that the ID in the R.java file adds a constant named button to the static inner class, which is the identifier of the resource. If the ID of this static inner class does not exist, it will be generated first.

R.java File Description

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.