Use of system resources for android development tutorial android resource files

  • 2020-05-24 06:08:35
  • OfStack

1. Color resources
1. Color XML file format


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

2. Reference format:

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

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

2. String resources

1. XML file format


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


2. Reference format
java code: R.string.string_name
In the XML file: @string /string_name
3. Get the string: Resources.getString(int); Returns an object of type String

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

3. Size resources

1. XML file:


<dimen name=dimen_name> dimen_value</dimen>

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

2. Reference format
java code: R.dimen.dimen_name
In the XML file: @dimen /dimen_name
3, Resources. getDimension (R. dimen. dimen_name);
Note that using this method returns a value of type float, whereas setWidth() parameter 1 normally requires type int, so do a cast.

4. XML resource file

1. XMl file:
The xml file 1 that comes with the android program is normally located at values/xml/ xml_name.xml, where new- is required > file bed build 1 new xml file;
Version of the xml file


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

2. R.xml.xml_name in java code
3. Resources.getXml(int resid) An XmlResourceParser object is returned


Related articles: