5 years ago (2015-05-01)  Software development |   First to comment  6 
post score 0 times, average 0.0

First, Android resource classification explain


1, Android resource classification

The resources in Android are divided into two categories: resources that can be directly accessed, and native resources that cannot be accessed directly; -- direct access to resources: These resources can be accessed using R. They are all stored in the res directory. When compiling, The R.java resource index file will be automatically generated; -- Native resources: These resources are stored under assets, they cannot be accessed using R class, and can only be read as binary streams via the AssetManager.

2, Android resources explain


Resource details under the res directory:

  • -- res/animator : The property animation of the XML file directory. The property animation is the animation that defines the start, finish, repeat time and other parameters;
  • --res/anim : The tween file's XML file directory. Tweening animation defines only the starting and ending frames. The middle action is calculated by the system to obtain the action process.
  • --res/color : list of XML files in the color list;
  • -- res/drawable : Stores the real bitmap file. It can also store XML files of Drawable objects and subclass objects.
  • --res/layout : interface layout file directory;
  • -- res/menu : The menu file directory used in the program. The XML file under this file can be applied with the options menu, submenus, context menus, etc.
  • -- res/raw : Stores native resources. It has the same effect as the assets directory. You can get the binary input stream by calling the openRawResource(int) method. Similar to asstes, use the AssetsManager to access these resources.
  • -- res/value : store string. Integer, color, array The directory of the information XML file. The root element of these XML file resources is resource;
  • --res/xml : native XML file that can be accessed using the Resource.getXML() method;

Res/value directory XML file sub-tag parsing :

  • --string tag : represents a string;
  • -- integer tag : represents an integer value tag;
  • -- bool tag : represents a boolean value;
  • --color tag : represents a color value;
  • --array tag: represents an array;
  • -- string-array tag : represents an array of strings;
  • -- int-array tag : represents an int array;
  • --style tag : stands for style;
  • -- dimen tag : represents a size;

3, classified resource files:

If all the resources are put into an XML file, it will increase the difficulty of maintenance. Here, different types of resources will be placed in different XML files;

  • --arrays.xml : store array resources;
  • -- colors.xml : store color resources;
  • -- dimens.xml : store size value resources;
  • --strings.xml : store string resources;
  • --styles.xml : store style resources;

Second, the use of resources in Android


(1) Java code access inventory resources

Get resource syntax via R class in Java code:

  • -- pakegeName : The name of the package where the R class resides, that is, the type of permission. The R class may come from two packages, one is the manifest file of the program itself, and the other is a manifest file existing in the Android system;
  • -- resourceType : The name of the resource type represented in the R class, R.string string resource, R.integer representing an integer resource;
  • -- resourceName : the resource name. This resource name is the value of the name attribute of the corresponding child tag.

(2) Java code to access native resources

  • Resource class: Android resource access control class. This class provides a large number of methods for obtaining actual resources. Resource is obtained through the Context.getResource() method.
  • -- Get list resource : resource.getString(id), Get actual resource based on id;
  • -- Get native resources : resource.getassets(), get the AssetManager object;

(3) Using Resources in XML Files You need to reference the values ​​in other XML files in the XML file. The syntax is:

  • -- packageName : The package where the resource type is located. If the referenced resource is in the same package as this XML file, the package name can be omitted.
  • -- resourceType : resource type, such as layout, drawable, strings, etc.
  • -- resourceName : the resource name, the android:name attribute of the element in the XML file where the resource is located, or the file name without a suffix, such as a picture, a layout file, etc.;

Third, the string color size array resource usage

(1) Catalog Reference Names for Several Resources

String resources:

  • -- default directory : /res/values/strings.xml ;
  • -- References : R.string.xxx ;

Color resources:

  • -- default directory : /res/values/colors.xml ;
  • -- References : R.color.xxx ; Size Resources :
  • -- default directory : /res/values/dimens.xml ;
  • -- Reference : R.dimens.xxx ;
  • Color definition method:
  • 1. Three primary colors: White light can be decomposed into light of red, green and blue colors. When red, green and blue are the maximum, they are white. The three values ​​are equal, but not the maximum is gray. If one or both The value is relatively large, it will produce a variety of colors;
  • 2. Color representation: Colors are represented by red, green, blue, and alpha (alpha); -- Colors begin: Color values ​​always start with #; -- No transparency: If there is no alpha value, the default is completely opaque; the color definition form:
  • -- #RGB : Red, Green, Blue Three primary color values, each value is divided into 16 levels, the minimum is 0, the maximum is f;
  • -- #ARGB : Transparency Red Green Blue Values, each value is divided into 16 levels, the minimum is 0, the maximum is f;
  • -- #RRGGBB : Red, green, and blue primary color values, each value is divided into 256 levels, with a minimum of 0 and a maximum of ff;
  • -- #AARRGGBB : Transparency Red Green Blue value, each value is divided into 256 levels, the minimum is 0, the maximum is ff;

(2) string color size XML file definition


1) String resource file


  • String resource file information:
  • -- Resource location : /res/values ​​directory;
  • -- Root element : is the root element;
  • -- Child elements : ;
  • --name attribute : Specify the variable name;
  • -- Label text : Label text is string information;


2) Color resource files

Color resource file information:

  • -- Location : /res/values ​​directory;
  • -- Root element : ;
  • -- Child elements : ;
  • --name attribute : color resource name;
  • -- Label text : color value;


3) Size resource file

  Size resource information:

  • -- Location : /res/values ​​directory;
  • -- Root element : ;
  • -- Child elements : ;
  • --name attribute : size name;
  • -- Label text : Size value;

4) array resources

Resource Array File : The array is usually defined in the /res/values/arrays.xml file;

  • -- Root tag : ;
  • -- Subtag : , , ;

Resource array types : Array resources are followed by , and different types of arrays have different child elements.

  • -- Normal type array : Used as a child element tag;
  • -- An array of strings : used as child element tags;
  • -- Integer array : Used as a child element tag;

Call the array resource in the XML file: @[packageName :] array/arrayName ; Call the array resource in the Java file: [packageName . ]R.array.arrayName ;

  • -- Get the actual ordinary array : TypeArray obtainTypedArray(int id), Get the actual ordinary array based on the common array resource name, The TypeArray class provides the getXxx(int ​​index) method to get the element with the specified index;
  • -- Get an array of strings : String[] getStringArray(int id), Get an array of strings based on the string array resource name;
  • - Get an array of integers : int[] getIntArray(int id), Get the actual array of integers based on the integer array resource name;

This article has been printed on copyright and is protected by copyright laws. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click the "Sponsoring Author" below to call the author!

Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/android-resources-detailed-in-primary-segment-2.html

Post comment


No Comment


Forget password?