Resources are very important in the Android system, and the resources commonly used are strings,colors,bitmaps and layouts , so you can modify the values of these files without having to recompile the program. There are a lot of resource types in Android, here we mainly discuss the types of learning commonly used.
Android Strings files are placed under the Res/values directory, you can define one or more XML files to hold the strings value, the file name can be arbitrary, but often you see the file name is
Strings.xml.
<?XML version= "1.0" encoding= "Utf-8"?><Resources><stringname= "Hello">Hello</string><stringname= "App_name">Hello appname</string></Resources>
When used in the program: R.string.hello; R.string.app_name.
The layout of Windows in Android is usually done through an XML file, and the files are generally placed under the Res/layout directory.
Public class extends activity{@Overridepublicvoid onCreate (Bundle savedinstancestate) {super . OnCreate (savedinstancestate); Setcontentview (R.layout.main); = (TextView)this. Findviewbyid (R.ID.TEXT1), Tv.settext ("Try this text instead");} ...}
The red part of the code shows that the layout file that sets this activity's view is the Main.xml file under Layout.
For example, the Main.xml file code is as follows:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"><TextViewAndroid:id= "@+id/text1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello"/><ButtonAndroid:id= "@+id/b1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello"/></LinearLayout>
The reference syntax structure for resource is: @[package:]type/name. Used in
- R.string
- R.id
- R.drawable
- R.layout
- R.attr
- R.plural
- R.array
There are some important file types in the Res directory we need to know:
- ANIM: Compile the animation file.
- Drawable:bitmaps.
- Layout: Define UI and view.
- Values:arrays,strings,colors,dimensions and styles.
- Xml:xml file.
- Raw:noncompiles RAW files.
- Use of String arrays
<Resources....>...... Other Resources<String-arrayname= "Test_array"><Item>One</Item><Item>Both</Item><Item>Three</Item></String-array>...... Other Resources</Resources>
When referencing in code:
// Get access to Resources object from an Activity Resources res = your-= Res.getstringarray (R.array.test_array);
<Resources ...><pluralsname= "Eggs_in_a_nest_text"><ItemQuantity= "One">There is 1 egg</Item><ItemQuantity= "Other">There is%d eggs</Item></plurals></Resources>
Resources res = your-= res.getquantitystring (R.plurals.eggs_in_a_nest_text, 0,0= Res.getquantitystring (r.plurals.eggs_in_a_nest_text,= res.getquantitystring (r.plurals.eggs_in_a_ Nest_text, 2,2= res.getquantitystring (R.plurals.eggs_in_a_nest_text, 10,10);
Android Learning II: Resources