Introduction to major resource files and folders in Android _android

Source: Internet
Author: User
Tags getcolor xml attribute xml example java reference
In the Android project folder, the main resource files are in the Res folder.

1:assets folder is stored without compiling the original file, that is, the folder inside the file will not be like Xml,java file is precompiled, you can store some pictures, Html,js, CSS and other documents.
Introduction to the 2:res folders inside multiple folders

Res/anim/xml files, which are compiled into a frame of animation (frame by frame animation) or motion tween (tweened Animation) object res/drawable/. png,. 9.png,. jpg files, They are compiled into the following drawable resource subtypes, in order to obtain a resource of this type, you can use the resource.getdrawable (ID), and the mcontext.getresources () to obtain the resource type. Getdrawable (R.drawable.imageid) Note: The image resources placed here may be automatically optimized for lossless compression by the AAPT tool. For example, a PNG that is true in color but does not require 256 colors may be converted to a 8-bit PNG with a palette. This makes the image of the same quality occupy less resources. So we have to be aware that these binary images that are placed in this directory may change as they are generated. If you want to read an image bit stream and convert it to a bitmap (bitmap), place the image file in the res/raw/directory so that you can avoid being automatically optimized.

An XML file that is compiled into a screen layout (or part of a screen) res/layout/

Res/values/can be compiled into XML files of many types of resources. Note: Unlike other res/folders, it can hold any number of files that hold a 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 arbitrarily named, the following makes some of the more typical files (file naming conventions include element types within that name):

Array.xml definition Array

Colors.xml defines a string value for color drawable and colors (color string values)

These resources are obtained using resource.getdrawable () and Resources.getcolor () respectively.

Dimens.xml defines the dimension value (dimension value).

Use Resources.getdimension () to obtain these resources.

Strings.xml definition String (string) value

Use Resources.getstring () or Resources.gettext () to obtain these resources.

Styles.xml-Defined Style object

res/xml/any XML file that can be read by calling Resources.getxml () at run time.

res/raw/Any file that is copied directly to the device. They do not need to be compiled and added to the compressed file generated by your application's compilation. To use these resources, you can call Resources.openrawresource (), which is the ID of the resource, or R.raw.somefilename.

The automatically generated R class has a R.java in the project folder's Gen folder, and the resources we normally refer to are mainly references to variables of this class. Note: The R class is automatically generated, and it cannot be manually modified. When a resource changes, it is automatically modified.

Using resources in your code the following is a resource-referencing syntax: R.resource_type.resource_name or Android. R.resource_type.resource_name where Resource_type is a subclass of R that holds a specific type of resource. Resource_name is the name attribute of the resource defined in the XML file, or there are other file types that are defined for the resource (without the extension, which refers to a icon.png similar file in the Drawable folder, Name=icon). Android contains a number of standard resources, such as screen styles and button backgrounds. To refer to these resources in your code, you must use Android to qualify, such as Android. R.drawable.button_background.

referencing resources within an XML file

1 Referencing a custom resource

android:text= "@string/hello" uses the "@" prefix to introduce a reference to a resource--the text that follows in @[package:]type/name form is the name of the resource. In this case, we do not need to specify the package name, because we refer to the resources in our own package. Type is the XML child node name, name is the XML attribute name: Hello World, hellodemo!

2) referencing system resources android:textcolor= "@android: color/opaque_red" specified package:android

3 Referencing Subject attributes Another resource value allows you to refer to the value of the attribute in the current topic. This property value can only be used in style resources and XML attributes, and it allows you to change the appearance of UI elements by changing them to the standard changes provided by the current topic, rather than providing specific values. Android:textcolor= "Android:textdisabledcolor" Note that this is very similar to resource references except that we use a "?" Prefix instead of "@". When you use this tag, you provide the name of the attribute resource, which will be looked up in the topic-because the resource tool knows the required attribute resource, so you do not need to display the declaration of that type (if the declaration is in the form of a android:attr/android: Textdisabledcolor). In addition to using the resource's identifier to query the values in the topic instead of the original resource, the naming syntax is consistent with the "@" form:? [Namespace:]type/name, this type is optional.

Color value Syntax: #color_value can be saved in Res/values/colors.xml (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 (declaring two colors, first opaque, second transparent): #f00 #80ff0000

Color drawables Syntax: Color_value can be saved in Res/values/colors.xml.

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

Java references: drawable reddrawable = resources.getdrawable (r.drawable.color_name) Color_name is the same as above.

The picture is generally placed in the res/drawable/official note png (preferred), JPG (acceptable), GIF (discouraged), it seems that the general use of PNG format better!

XML Reference @[package:]drawable/some_file

Java reference r.drawable.some_file references are not with extensions

Dimension syntax: Dimen_value units are generally saved as res/values/dimen.xml.

Unit of measure:

PX (pixels): The actual pixel screen, often said resolution 1024*768pixels, is horizontal 1024px, vertical 768px, different devices display the same effect. In (inches): The physical dimensions of the screen, equal to 2.54 centimeters per inch. MM (mm): The physical dimensions of the screen. PT (Point): The physical dimensions of the screen. 1/72 inches. Dp/dip: A pixel that is independent of density, an abstract unit based on screen density. 1DP = 1px on display at 160 dots per inch. But the ratio of DP and PX will change with the change of screen density, different devices have different display effects. SP: A scale-independent pixel, used primarily for font display best for TEXTSIZE, as a text-related size unit.

Xml:android:textsize= "@dimen/some_name"

Java:float dimen = resources.getdimen (r.dimen.some_name)

Assets Folder Resources Access assets folder files are kept in the original file format, you need to use the Assetmanager as a byte stream to read the file.
1. Call Getassets () first in the activity to get the Assetmanager reference.
2. The Assetmanager open (String fileName, int accessmode) method specifies the read file and access mode to get the input stream inputstream.
3. Then, read the file with the InputStream of the open file, and remember Inputstream.close () when it is finished.
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.