Android Auto-generated R class

Source: Internet
Author: User

The use of resource files is divided into code that is used in the resource file and referenced in other resource files. When we compile an Android app, Android automatically generates an R class that generates the corresponding inner class based on different resource types, which contains the identifiers of all the resource files used in the system.

  

1. Using resource files in your code

Accessing the resource file in code is accessed by using the resource file type and resource file name defined in the R resource class. The specific format is: R. resource file type. resource file name. For example:

Java code:
Set layout view for activity display
Setcontentview (R.layout.login_system);
Get Button Instance
CANCELBTN = (Button) Findviewbyid (R.id.cancelbutton);
LOGINBTN = (Button) Findviewbyid (R.id.loginbutton);
Get TextView instances
Useredittext = (EditText) Findviewbyid (R.id.useredittext);
Pwdedittext = (EditText) Findviewbyid (R.id.pwdedittext);

}


       Additionally, you can access resource files in the system, in addition to accessing the user's own defined resource files. Most of the resource files are defined in the R class under the Android package. The resource file format in the Access system is: Android. R. resource file type. resource file name. For example:

java code:
int i;  
//animation  
i = Android. r.anim.fade_in; 
//array  
i = Android. r.array.emailaddresstypes; 
//color  
i = Android. r.color.darker_gray; 
//Size  
i = Android. r.dimen.app_icon_size; 
//can draw picture  
i = Android. r.drawable.title_bar; 
//String  
i = Android. R.string.cancel;


2. Referencing resource files in other resource files

We often refer to other resource files in the element properties of the layout file, often using resources such as strings, pictures, colors, and so on. For example, the TextView component in the following layout file refers to a string representing the text content, referencing a color to represent the text color, and referencing a dimension to represent the text size.

Java code:
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/styled_welcome_message"
Android:textcolor= "@color/opaque_red"
Android:textsize= "@dimen/sixteen_sp"/>

Transferred from: http://android.tgbus.com/Android/tutorial/201105/353458.shtml

Android Auto-generated R class

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.