SoFunction
Updated on 2025-03-11

Android development tutorial: How to use system resources android resource files

1. Color resources
1. Color XML file format

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources> //resources root element
   <color name="" >#000000</color>
//color child element
</resouces>

2. Quote format:

In the java code: .color_name  (This is an int type data, used to represent the address of the resource)
In the XML file: @color/color_name

3. Method to obtain color value getResources().getColor();   Return a color value similar to #0000FF

2. String resources

1. XML file format

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources> //resources root element
<string name="" >StringHere</string>
//string child element
</resouces>


2. Quote format
In java code: .string_name
In XML file: @string/string_name
3. Method to obtain string: (int);    Return an object of type String

Note that in the control, both setText(int resid)   and setText(String string_name)    are allowed.

3. Size resources

1. XML file:

Copy the codeThe code is as follows:

<dimen name=dimen_name> dimen_value</dimen>

The unit of size value can be px (pixels) or mm (mm, actual screen size) or dp

2. Quote format
java code:  .dimen_name
In the XML file: @dimen/dimen_name
3、(.dimen_name);
Note that using this method returns a float type value, and the setWidth() parameter generally requires that it is of type int, so a cast must be performed.

4. XML resource files

1. XML file:
The xml file that comes with the Android program is usually placed at values/xml/xml_name.xml . Here you need to use the new->file bed to create a new xml file;
Version of xml file

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>

2. .xml_name in java code
3. (int resid );   Returns an XmlResourceParser object