Introduction to main resource files and folders in Android

Source: Internet
Author: User
Tags getcolor xml attribute xml example java reference

In the Android project folder, the main resource files are stored in the res folder.

1: The assets folder is a native file that is not compiled and processed, that is, the files in this folder are not like xml, java files are pre-compiled, and some images, html, js, and, css files.
2: Introduction to Multiple folders in the res folder

Res/anim/XML files, which are compiled into frame-by-frame animation or tweened animation (res/drawable/.pngw..9.pngw.jpg) objects, they are compiled into the following Drawable Resource child types. To obtain a Resource of this type, you can use Resource. getDrawable (id); to obtain the resource type, use mContext. getResources (). getDrawable (R. drawable. imageId) Note: image resources placed here may be automatically compressed and optimized by aapt. For example, a PNG with a true color but no 256 color is required may be converted into an 8-bit PNG with a color palette. This allows images of the same quality to consume less resources. So we must realize that these binary images placed in this directory may change during production. If you want to read an image bit stream and convert it into a bitmap, put the image file in the res/raw/directory to avoid automatic optimization.

Res/layout/XML file compiled as a screen layout (or part of the screen)

Res/values/can be compiled into XML files of many types of resources. Note: unlike other res/folders, it can save any number of files that save the description of the resource to be created, rather than the resource itself. The XML Element type controls where these resources should be placed in the R class. Although the files in this folder can be named arbitrarily, some typical files are used below (the naming convention of a file is to include the element type in the name ):

Array. xml defines an array

Colors. xml defines the color drawable and color string values (color string values)

Use Resource. getDrawable () and Resources. getColor () to obtain these Resources respectively.

Dimens. xml defines the dimension value ).

Use Resources. getDimension () to obtain these Resources.

Strings. xml defines the string value

Use Resources. getString () or Resources. getText () to obtain these Resources.

Styles. xml defines a style object

Res/xml/any XML file can be read by calling Resources. getXML () at runtime.

Res/raw/directly copy to any file on the device. They do not need to be compiled and are added to the compressed files generated by your application compilation. To use these Resources, you can call Resources. openRawResource (). The parameter is the resource ID, that is, R. raw. somefilename.

The automatically generated R class has an R. java in the gen folder of the project folder. The resources we usually reference mainly the variables of this class. Note: The R class is automatically generated and cannot be manually modified. It is automatically modified when the resource changes.

To use resources in code, the following is a syntax for referencing resources: R. resource_type.resource_name or android. R. resource_type.resource_name. resource_type is a subclass of R and stores a specific type of resources. Objects, name = icon ). Android contains many standard resources, such as screen styles and button backgrounds. To reference these resources in the code, you must use android for limitation, such as android. R. drawable. button_background.

Reference resources in xml files

1) Reference custom Resources

Android: text = "@ string/hello" use the "@" prefix to introduce a reference to a resource -- In @ [package:] the text following the type/name format is the name of the resource. In this case, we do not need to specify the package name, because we reference the resources in our own package. Type is the xml subnode name, and name is the xml Attribute name: Hello World, HelloDemo!

2) Reference System Resources android: textColor = "@ android: color/opaque_red" specify package: android

3) referencing a topic attribute another resource value allows you to reference the attribute value in the current topic. This attribute value can only be used in style resources and XML attributes. It allows you to change the appearance of the UI element by changing them to the standard changes provided by the current topic, rather than providing specific values. Android: textColor = "? Android: textDisabledColor "NOTE: This is very similar to resource reference, except that we use "? "Prefix replaces "@". When you use this tag, you provide the name of the property resource, which will be searched in the topic-because the resource Source Tool knows the required property resource, so you don't need to display the Declaration type (if it is declared, the form is? Android: attr/android: textDisabledColor ). Except that the resource identifier is used to query the value in the topic to replace the original resource, its life name syntax is consistent with the "@" Format :? [Namespace:] type/name. The type is optional.

Color Value Syntax: # color_value can be stored in res/values/colors. xml (the file name can be arbitrary ).

Xml reference: android: textColor = "@ color/color_name"

Java reference: int color = Resources. getColor (R. color. color_name)

Where # color_value has the following format (A represents the Alpha channel): # RGB # ARGB # RRGGBB # AARRGGBB xml example (declare two colors, the first is not transparent, and the second is transparent ): # f00 #80ff0000

Color Drawables Syntax: color_value can be stored in res/values/colors. xml.

Xml reference: android: background = "@ drawable/color_name"

Java reference: Drawable redDrawable = Resources. getDrawable (R. drawable. color_name) color_name is the same as above.

Images are generally placed in res/drawable/. The official prompts include png (preferred), jpg (acceptable), and gif (discouraged). It seems that the png format is better!

Xml reference @ [package:] drawable/some_file

Java references the R. drawable. some_file reference without the extension

Dimension Syntax: The dimen_value unit is generally saved as res/values/dimen. xml.

Measurement unit:

Px (pixel): The actual pixel of the screen. The resolution is usually 1024*768 pixels, that is, 1024px in the horizontal direction and 768px in the vertical direction. Different devices have the same display effect. In (English): the physical size of the screen. each inch equals 2.54 cm. Mm (mm): physical size of the screen. Pt (point): physical size of the screen. 1/72 inch. Dp/dip: density-independent pixel, an abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch. However, the ratio of dp to px varies with the screen density, and different devices have different display effects. Sp: pixel irrelevant to the scale. It is mainly used to display the best for textsize in the font, and is used as the unit of size related to the text.

XML: android: textSize = "@ dimen/some_name"

Java: float dimen = Resources. getDimen (R. dimen. some_name)

The files in the assets folder to access resources are in the original file format. You need to use AssetManager to read files in byte streams.
1. Call getAssets () in the Activity to obtain the AssetManager reference.
2. Use AssetManager's open (String fileName, int accessMode) method to specify the file to be read and the access mode to obtain the input stream InputStream.
3. Use inputStream of the open file to read the file. After reading the file, remember inputStream. close ().
4. Call AssetManager. close () to close AssetManager.

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.