Usage overview of Chinese-funded Android source files (non-code Part) _ jquery

Source: Internet
Author: User
Resources in Android refer to non-code parts and external files. This document provides a detailed overview of resource usage. For more information, see Resource Access in Android
Resources in Android refer to non-code parts and external files.
Generally, native files are stored in assets. For example, MP3 files cannot be directly accessed by Android programs. They must be read in the form of binary streams through the AssetManager class.
Resources in res can be directly accessed through the R Resource class.
The R class is automatically generated, and the corresponding internal class is generated based on different resource types. This class contains the identifiers of all resource files used in the system.

1. Use resource files in code
Accessing resource files in code is accessed by the resource file type and resource file name defined in the R class.
The specific format is:
R. Resource file type. Resource file name
In addition to accessing user-defined resource files, you can also access resource files in the system.
The format of resource files in the access system is:
Android. R. Resource file type. Resource file name

2. Reference The resource file in other resource files
Images, color resources, string resources, and size resources are often referenced in layout files.
The general format of referenced resources in other resources is:
@ [Package name:] Resource Type/Resource Name

Use color Resources
At the beginning of the color value definition, there is a #, followed by the Alpha-RGB format.
For example:
# RGB
# ARGB
# RRGGBB
# AARRGGBB
Format of referenced Resource:
In Java code: R. color. color_name
In the XML file: @ [package:] color/color_name
Define a colors. xml file under the res \ values \ directory to store key-value pairs of color names and color values.
For example:

The Code is as follows:



# F00
# 0000ff


Other resources such as strings and dimensions are similar methods.

Use string Resources
Create a string resource file strings. xml. The content is also a key-Value Pair
Reference string resource R. string. string_name in Java code
You can obtain the following information:
String str = getString (R. string. string_name). toString ();
Reference string resource in xml file: @ [package:] string/string_name

Use dimension resources
Size resources are defined in the res \ values \ dimens. xml file.
Size Units supported in Android:

Unit Representation

Organization Name

Description

Dip

Device Independent Pixel

The dip varies with the screen density of different devices.

Px

Pixels

The real pixel representation on the screen, the display effect of different devices is the same

In

Feet

Screen-based physical size

Mm

Millimeters

Screen-based physical size

Pt

Point (lbs)

1/72 feet

Dp

Density-independent pixels

Abstract unit of relative screen physical density

Sp

Pixels unrelated to precision

Similar to dp, pixels irrelevant to the scale mainly process the font size.


Reference dimension Resource:
In Java code: R. dimen. dimen_name
In the xml file: @ [package:] dimen/dimen_name

Use original XML Resources
The XML file is defined in the res \ xml \ directory of the project and accessed through the Resources. getXML () method.
The idea of getting the original XML file is to use getResources (). get the original XML file by getXml () and get the XmlResourceParser object. You can use this object to determine whether it is the start or end of a document, the start or end of a tag, you can use some methods to obtain attributes to traverse the XML file and access the content of the XML file.

Use drawables Resources
A drawable resource is an image or color resource used to draw a screen. It is obtained through the Resources. getDrawable () method.
Drawable resources are classified into three types: Bitmap File, Color Drawable, and Nine-Patch Image ).
Supported bitmap files in Android include png, jpg, and gif.
Format of the referenced bitmap Resource:
In Java code: R. drawable. file_name
In the XML file: @ [package:] drawable/file_name

Use layout Resources
Layout resources are the most commonly used resources in Android. The layout of components on the screen is defined in an XML file, similar to HTML pages on the Web.
The layout file is located in res \ layout \ with any name. Android parses the components in the XML file into visualized view components through the LayoutInflater class.
In the Activity, call Activity. setContentView () to set the layout file to the Activity interface, and use the findViewById () method to obtain the layout component.
Reference layout file:
In Java code: R. layout. my_layout
In the XML file: @ [package:] layout/my_layout

Use menu Resources
You can create any view component by declaring it in the layout file, or by creating it in code.
Menus in Android include option menus, context menus, and sub-menus, which can be declared and defined in the XML file and used in the Code through the MenuInflater class.
The menu resource file is also an XML file, which is placed in the res \ menu \ directory of the project. Use the R. menu. my_menu method to reference.
Structure of a typical menu resource file: Root element, nested in the root element And Child element, Element can also be nested Create a sub-menu.
Due to limited time and energy, we cannot do a detailed example study for each type, so we have to combine it in practical applications and then try again. Come on!
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.