Introduction and use of Android resource files

Source: Internet
Author: User

Introduction and use of Android resource files

1. Resource introduction:
Defined in the res Directory: strings, colors, arrays, menus, images, videos, etc.; use these resources in applications;

2. advantages of using resources: reduces the amount of code and facilitates later maintenance;

3. Resource types:
Directory resource type description
Res/anim xml animation information, frame animation or this complementary Animation
Res/layout xml save layout information
Res/values xml string, color, size, type, and topic. The file can be named in the form of key-value. We recommend that different files store different types of values;
Res/menu xml saves menu resources. A resource file represents a menu
Res/xml is used to save any xml file, which can be read by code Resources. getXML;

Resources in the res/raw directory of any type are not compiled. You can call Resource. openRawResource (int id) to obtain the binary input stream of the Resource;
Assets of any type are the same as raw and will not be compiled. The difference is that the resource source files in this directory will not generate resource IDs;
Res/drawable graphics save images in multiple formats


4. system resources:
Android contains a large number of system resources, which are stored in the/platforms/android-version/data/res directory of the SDK;
Use: andorid. R. Resource file type. Resource ID in the code, reference in the XML file: @ android: Resource Type/resource ID;

5. How to use custom resources:
For example, how to use color resources ....
Resource location res/values/colors. xml
Color XML file format
Root element
Child element: # Color_value
Resource. getColor ();
Format of referenced Resource: in 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
Root element
String_value
Method for obtaining string Resources. getString ();

Reference string in java: R. string. string_name = key
In xml: @ string/string_name

7. Use size resources:

Resource location: res/vaules/dimens. xml
String xml file format
Root element
Dimen_value

GetResources (). getDimension ()

Reference string in java: R. dimen. dimen_name = key
In xml: @ dimen/dimen_name


8. Use drawables resources:
A drawable resource is an image or color resource used to draw a screen. It is obtained through Resourcs. getDrawable (): method;
Drawable resources are classified into three types: BitmapFile (bitmap file), Color Drawable (Color), and Nine-Patch Image (Nine images );
Supported bitmap files for android include: png, jpg, and gif.

The usage is the same as above;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.