Introduction to major resource files and folders in Android

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

In the Android project folder, the primary resource file is placed in the Res folder.

The 1:assets folder is a native file that is not compiled and processed, that is, the files inside the folder will not be precompiled like Xml,java files, and can store some pictures, Html,js, CSS, and other files.
2:res folders within the folder of the respective introduction

Res/anim/xml files, which are compiled into frame-by-frames animations (frame by frame animation) or tweened animation (tweened animation) objects res/drawable/. png,. 9.png,. jpg files, They are compiled into the following drawable resource subtypes, and to obtain a resource of this type, you can use Resource.getdrawable (ID), and mcontext.getresources () in order to get the resource type. Getdrawable (R.drawable.imageid) Note: The image resource placed here may be automatically optimized for lossless compression by the AAPT tool. For example, a true color but does not require a 256-color PNG may be converted to a 8-bit PNG with a palette. This allows images of the same quality to occupy less resources. So we have to realize that these binary images placed in this directory may change at the time of generation. 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.

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

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 the description of the resource to be created, not 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 are some typical files (the Convention for file naming is to include element types in the name):

Array.xml Defining arrays

Colors.xml A string value that defines color drawable and colors (color string values)

Use Resource.getdrawable () and Resources.getcolor () to obtain these resources separately.

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 Defining Style objects

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

res/raw/copy directly to any file in the device. They do not need to be compiled and added to the compressed file generated by your application compilation. To use these resources, you can call Resources.openrawresource (), which is the ID of the resource, which is r.raw.somefilename.

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

Using resources in your code the following is a syntax for referencing resources: 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 property of the resource defined in the XML file, or there are other file types that have the file name defined for the resource (not including the extension, which refers to the icon.png similar file inside the drawable folder, Name=icon). Android contains a number of standard resources, such as screen styles and button backgrounds. To reference these resources in your code, you must use Android to qualify them, such as Android. R.drawable.button_background.

referencing resources within an XML file

1) referencing a custom resource

android:text= "@string/hello" here uses the "@" prefix to introduce a reference to a resource-the text that follows in the @[package:]type/name form is the name of the resource. In this case, we do not need to specify the package name because we are referencing 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" Specifies package:android

3) Referencing a topic property Another resource value allows you to reference the value of a property in the current theme. 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 theme, 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 property resource, which will be looked up in the subject-because the resource tool knows the required property resource, so you do not need to display the claim type (if the declaration, its form is? Android:attr/android: Textdisabledcolor). In addition to using the identifier of this resource to query the values in the topic instead of the original resource, its naming syntax is the same as "@":? [Namespace:]type/name, this type is optional.]

The Color value syntax: #color_value can be saved in res/values/colors.xml (filename can be arbitrary).

XML reference: android:textcolor= "@color/color_name"



Where #color_value has the following format (a for 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 reference: drawable reddrawable = resources.getdrawable (r.drawable.color_name) Color_name is the same as above.

Pictures generally placed inside the res/drawable/official hint png (preferred), JPG (acceptable), GIF (discouraged), it seems that the general use of PNG format is better!

XML Reference @[package:]drawable/some_file

Java Reference R.drawable.some_file Reference is not with extension

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

Unit of measure:

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

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



Assets folder resource Access Assets folder files are kept in the original file format, you need to use Assetmanager as a byte stream to read the file.
1. First call Getassets () in the activity to get the Assetmanager reference.
2. The Assetmanager open (String fileName, int accessmode) method specifies the read file and the access mode to get the input stream inputstream.
3. Then you read the file with the InputStream of the open file, and remember to Inputstream.close () after the read is done.
4. Call Assetmanager.close () to close Assetmanager.

Note: This article excerpt from http://www.jb51.net/article/39021.htm, if there is infringement please contact Delete.

Introduction to major resource files and folders in Android

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.