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