Introduction and use of Android resource files
1, the resources of the introduction:
defined in the res directory: strings, colors, arrays, menus, pictures, videos, etc.; Use these resources in the application ;
2, the advantages of using resources: Reduce the amount of code, at the same time for the later maintenance also brought convenience;
3, the type of resources:
Directory Resource type description
Res/anim XML animation information, frame animation, or this motion tween
res/layout XML Saving layout information
res/values XML string, color, size, type, theme, etc., the file can be appointed name, in the form of Key-value , suggest different files to save different types of values;
res/menu XML Save menu resource, a resource file represents a menu
res/xml XML is used to save arbitrary XML files, which can be read by code resources.getxml () ;
Res/raw Resources in any type of directory are not compiled, you can call resource.openrawresource (int id) to get the binary input stream of the resource;
assets Any type , like Raw, is not compiled, unlike resource files in the directory that do not generate resource IDs;
res/drawable Graphics save images in multiple formats
4. System resources:
There are a lot of system resources in Android that are stored in the /platforms/android- version of the SDK /data/res in the catalogue;
used in code:Andorid. R. resource File Types . Resources ID , in XML referenced in file: @android: Types of resources / Resources ID ;
5. How to use a custom resource:
Example: How to use color resources ... .
Resource Location res/values/colors.xml
The color XML file format uses <?xml version= "1.0" encoding= "Utf-8"?>
<resources> root elements
<color> child elements:<color name= "Color_name" > #color_value </color>
method of obtaining color resource.getcolor ();
format of reference resource: java :r.color.color_name
in xml:@[package:]color/color_name
6. Use string resources:
Resource location: res/vaules/strings.xml
string XML file format <?xml version= "1.0" encoding= "Utf-8"?>
<resources> root elements
<string name=string_name>string_value</string>
method of obtaining a string resource resources.getstring ();
how strings are referenced in Java:r.string.string_name string_name==key
in xml:@string/string_name
7, the use of size resources:
Resource location: res/vaules/dimens.xml
string XML file format <?xml version= "1.0" encoding= "Utf-8"?>
<resources> root elements
<dimen name=dimen_name>dimen_value</dimen>
Gets the string resource method getresources (). Getdimension ()
how strings are referenced in Java:r.dimen.dimen_name dimen_name==key
in xml:@dimen/dimen_name
8. Use drawables resources:
drawable Resources are some pictures or color resources, mainly used to draw the screen, through the resourcs.getdrawable (): method obtained;
drawable Resources fall into three categories:bitmapfile ( bitmap file ),colordrawable ( colors ),nine-patch image ( nine pictures );
the bitmap files supported by Android are:png/jpg/gif
using the same method as above;