From: http://android.tgbus.com/Android/tutorial/201105/353458.shtml
Android Auto-generated R class
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 in which the corresponding inner class is generated based on the different resource types, which contains the identifiers of all the resource files used in the system, as shown in the following.
Java code:
Package Eoe.demo;
Resource class
Public final class R {
Array
public static final class Array {
public static final int faultrecords=0x7f060000;
}
Property
public static final class Attr {}
Color
public static final class Color {
public static final int black=0x7f040001;
public static final int red=0x7f040000;
}
Image
public static final class Drawable {
public static final int icon=0x7f020001;
public static final int logo2=0x7f020002;
}
ID Label
public static final class ID {
public static final int licenseedittext=0x7f070022;
public static final int lngedittext=0x7f070001;
}
Layout
public static final class layout {
public static final int custom_dialog=0x7f030000;
public static final int custom_dialog1=0x7f030001;
}
String
public static final class string {
public static final int app_name=0x7f050001;
public static final int hello=0x7f050000;
}
}
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"/>
Android Auto-generated R class