These days to summarize the user resource access in Android development.
User resources in Android project in the Res folder, there are strings, colors, sizes, arrays, layouts, styles, themes and other resources, these resources can be referenced in the XML file, can also be used in the Android source files, today summed up the string, color, size, array, Layout and picture resources.
In general, the format referenced in the XML file is [<package>.] @/xxx/name; The reference format in the source code is [<package>.] R.xxx.name.
The code referenced in the XML file (in the layout XML file) and in the source code are pasted first.
Main.xml layout file <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" android:id= "@+id/ LinearLayout1 " android:layout_width=" Match_parent " android: layout_height= "match_parent" android:orientation= "vertical" tools:context= "Main.testresources.MainActivity" > <TextView android:id= "@+id/tv1" Android:layout_width= "wrap_content" android:layout_height= "wrap _content " android:textcolor=" @color/lor " android:textsize= "@dimen/dim" android:text= "@string/TV1" android:background= "@drawable/ic_launcher" /> <button android:id= "@+id/btn1" android:layout_width= "Wrap_content" android: layout_height= "Wrap_content" android:text= "@string/btn1" android:textcolor= "@color/lor2" android:textsize= "@dimen/dim2" android:background= " @drawable/ic_launcher "/> <!-- above adt 16.0, in some controls without text display, you need to add a function description, i.e. android:contentdescription= "@string/jieshao"; android:src= "@drawable/test" is to set the picture display --> <imageview android:id= "@+id/iv1" &nbSp; android:layout_width= "Wrap_content" android: layout_height= "Wrap_content" android:contentdescription= "@ String/jieshao " android:src=" @drawable/test "/> <!-- Referencing an external layout file, which adds an external layout file to the layout file and becomes part of the layout file name layout_test.xml--> <include layout= "@layout/layout_test" /> </LinearLayout>
Layout_test.xml layout file for Main.xml file call <?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:id= "@+id/ LinearLayout1 " android:layout_width=" Match_parent " android: layout_height= "match_parent" android:orientation= "Horizontal" > <Button android:id= "@+id/button1" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/button1" /> <Button &nBsp; android:id= "@+id/button2" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/button2" /></LinearLayout>
Android source file public class mainactivity extends actionbaractivity implements onclicklistener{ protected textview tv1=null; protected button btn1=null; protected int num=0; protected StringBuffer stringBuffer=null; protected String[] arr=new string[3]; protected string str=null; protected int col=0; protected float dim=0; protected imageview iv=null; @SuppressLint ("NewApi") @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); setcoNtentview (R.layout.activity_main);//referencing the layout file and loading the display tv1= (TextView) Findviewbyid (R.ID.TV1); btn1= (Button) Findviewbyid (R.ID.BTN1); iv= (ImageView ) Findviewbyid (R.ID.IV1); str=getresources (). getString (R.STRING.TV1);//Get string Resources tv1.settext (str); col=getresources (). GetColor (R.color.lor);//Get Color resources Btn1.settextcolor (col);//Set text display color btn1.setbackgroundcolor (COL) ;//Set button background color dim =getresources (). getdimenSion (R.dimen.dim);//Get Size Resource btn1.settextsize (dim);//Set display text size arr= Getresources (). Getstringarray (R.array.world);//Get Array Resources Btn1.settext (arr[0]);//Take the first element of the array as a button to display the text tv1.setbackground (Getresources (). getdrawable (R.drawable.ic_launcher));//obtain picture resources, and set the background image iv.setimageresource (r.drawable.test);//Set Display picture btn1.setonclicklistener (mainactivity.this); }
(1), string resource
Existence position:/res/values/strings.xml, root element is <resources></resources>, with <string></string> tag, the following code:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >testresources</ string> <string name= "Hello_world" >hello world!</string> <string name= "Action_settings" >Settin gs</string> <string name= "TV1" > First </string> <string name= "BTN1" > Point I Toggle Show </string></ Resources>
(2), color resources
Existence position:/res/values/colors.xml, root element is <resources></resources>, with <color></color> tag, the following code
<?xml version= "1.0" encoding= "Utf-8"?><resources> <!--translucent, red-and <color name= "Lor" > #66ff000 0</color> <!--translucent, green-to <color name= "Lor2" > #6600ff00 </color></resources>
(3), size resources
Existence position:/res/values/dimens.xml, root element is <resources></resources>, with <dimen></dimen> tag, the following code
<resources> <!--Default screen margins, per the Android Design guidelines. --<dimen name= "Activity_horizontal_margin" >16dp</dimen> <dimen name= "Activity_vertical_margin" >16dp</dimen> <dimen name= "Dim" >30dp</dimen> <dimen Name= "dim2" >40dp</dimen></ Resources>
(4), array resources
Existence position:/res/values/arrays.xml, root element is <resources></resources>, with <integer-array></integer-array > or <string-array></string-array> tags, the following code
<?xml version= "1.0" encoding= "Utf-8"?><resources> <integer-array name= "Hello" > <item >1& lt;/item> <item >2</item> <item >3</item> </integer-array> <string -array name= "World" > <item >one</item> <item >two</item> <item >thre E</item> </string-array></resources>
(5), Layout file resources
Existence Position:/res/layout/xxx.xml, the root element is variable, generally is <linearlayout></linearlayout>, labeled with each component node, see the layout XML file posted above
(6), picture resources
Existence location:/res/drawable-xxxx/picture name, non-XML file
Access to user resources in Android (i)