How to Use Android app resources (array, color, size, String, Boolean, integer)

Source: Internet
Author: User
Tags color representation
1. Detailed explanation of resource classification in Android

1. Classification of Android resources

Resources in Android are divided into two categories: resources that can be directly accessed, and native resources that cannot be directly accessed;

-Direct access to resources: These resources can be accessed using R. They are all stored in the res directory. During compilation, the R.java resource index file will be automatically generated;

-Native resources: These resources are stored under assets and cannot be accessed using the R class. Resources can only be read as a binary stream through the AssetManager;

2. Detailed Android Resources

Detailed resources under res directory:
-res / animator: XML file directory of attribute animation, attribute animation is to define the start and end, action, repeat time and other parameters with complete animation;
-res / anim: The tween animation XML file directory, tween animation is only defined start and end frames, the intermediate actions are calculated by the system to obtain the action process;
-res / color: XML file directory of color list;
-res / drawable: store real bitmap files, 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 to the option menu, sub-menu, context menu, etc .;
-res / raw: Store native resources, which has the same effect as the assets directory.You can obtain the binary input stream by calling the openRawResource (int) method. Similar to assets, use AssetsManager to access these resources;
-res / value: store the string. Integer, color, array information XML file directory, the root element of these XML file resources is resource;
-res / xml: Native XML file, which can be accessed using Resource.getXML () method;

Parsing of subtags of XML files in res / value directory

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

3. Classified resource files:

If you put all the resources in one XML file, it will increase the maintenance difficulty. Here, you will put different types of resources under 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 list resource

Get the resource syntax through the R class in Java code:


[packageName.] R. resourceType. resourceName

-pakegeName: The package name where the R class is located, that is, the type of permissions. The R class may come from two packages, one is the manifest file of the program itself, and the other is the manifest file that exists in the Android system;

-resourceType: the name of the resource type represented in the R class, R.string resource, R.integer represents integer resource;

-resourceName: resource name, this resource name is the value of the name attribute of the corresponding type subtag;

(2) Java code access to native resources

Resource class: Android resource access control class. This class provides a large number of methods to obtain actual resources. Resource is obtained through the Context.getResource () method;

-Get list resource: resource.getString (id), get actual resource according to id;

-Get native resources: resource.getassets (), get AssetManager object;
// Get Resource resource, this method is executed in Activity
Resources resources = getResources ();
// Get string resources
String hello = resources.getString (R.string.hello_world);
// Get image resources
Drawable luncher = resources.getDrawable (R.drawable.ic_launcher);

(3) Use resources in XML files

In the XML file, you need to refer to the values in other XML files. The syntax format is:

@ [packageName:] resourceType / resourceName

-packageName: the package of the resource type, 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: resource name, the android: name attribute of the element in the XML file where the resource is located, or the file name without suffix, sample, layout file, etc .;
Three, string color size usage of array resources

(1) Directory reference names of several resources
String resources:
-Default directory: /res/values/strings.xml;
-Reference method: R.string.xxx;


Color resources:
-Default directory: /res/values/colors.xml;
-Reference method: R.color.xxx;

Size resources:

-Default directory: /res/values/dimens.xml;
-Reference method: R.dimens.xxx;

PS

Color definition method:
Three primary colors: White light can be decomposed into three colors of red, green and blue. When the red, green and blue are the maximum, it is white. The three values are equal, but the maximum value is gray. If one or two values are compared Large, it will produce a variety of colors;
Color representation: The color is represented by three colors of red, green and blue, and transparency (alpha);
-Color start: color value always starts with #;
-No transparency: If there is no alpha value, the default is completely opaque;
Color definition form:
-#RGB: Red, green and blue primary color values, each value is divided into 16 levels, the minimum is 0, the maximum is f;
-#ARGB: Transparency red green blue value, 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, the minimum is 0, the maximum is 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: The label text is the string information;

  
  
    ResourceTest
    Settings
    Hello world!
  

2) Color resource file
Color resource file information:
-Location: / res / values directory;
-Root element:;
-Child elements:;
-name attribute: color resource name;
-Label text: color value;
  
  
    # FF4000
    # 120A2A
    # 00FF00
    # FFFF00
  

3) Dimension resource file
Size resource information:
-Location: / res / values directory;
-Root element:;
-Child elements:;
-name attribute: size name;
-Label text: size value;
  
      
    16dp
    16dp
  

4) Array resources

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

-Root label:;

-Subtags:,,;

Resource array type: The resources and tags of the array are all different, and the sub elements of different types of arrays are different

-Ordinary type arrays: used as sub-element tags;

-String array: used as a sub-element label;

-Integer array: use as sub-element label;

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

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

-Obtain the actual ordinary array: TypeArray obtainTypedArray (int id), obtain the actual ordinary array according to the ordinary array resource name, the TypeArray class provides the getXxx (int index) method to obtain the element at the specified index;

-Get string array: String [] getStringArray (int id), get string array according to the string array resource name;

-Get integer array: int [] getIntArray (int id), get the actual integer array according to the integer array resource name;
Example:
colors.xml
  
  
    # F00
    # 0F0
    # 00F
    # 0FF
    # F0F
    # FF0
    # 07F
    # 70F
    # F70
dimens.xml
  
  
    8dp
    60dp
    66dp
    18sp
 
strings.xml
  
  
    Hello World, ValuesResTest!
    String, number, size, array resources
    F00
    0F0
    00F
    0FF
    F0F
    FF0
    07F
    70F
    F70
  
arrays.xml
  
  
      
        @ color / c1
        @ color / c2
        @ color / c3
        @ color / c4
        @ color / c5
        @ color / c6
        @ color / c7
        @ color / c8
        @ color / c9
      
      
        @ string / c1
        @ string / c2
        @ string / c3
        @ string / c4
        @ string / c5
        @ string / c6
        @ string / c7
        @ string / c8
        @ string / c9
      
      
        Java
        Ajax
        Android
      
  

Main interface xml
  
  
  
      
  
      
      
  
      
      
  

activity
package WangLi.Resources.Values;
  
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.Type
dArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
  
public class ValuesResTest extends Activity {
    / ** Called when the activity is first created. * /
    String [] texts;
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.main);
        texts = getResources (). getStringArray (R.array.string_arr);
        // Create a BaseAdapter object
        BaseAdapter ba = new BaseAdapter ()
        {
            public int getCount ()
            {
                return texts.length;
            }
              
            public Object getItem (int position)
            {
                return texts [position];
            }
              
            public long getItemId (int position)
            {
                return position;
            }
              
            public View getView (int position, View convertView, ViewGroup parent)
            {
                TextView text = new TextView (ValuesResTest.this);
                Resources res = ValuesResTest.this.getResources ();
                // Use the size resource to set the height and width of the text box
                text.setWidth ((int) res.getDimension (R.dimen.cell_width));
                text.setHeight ((int) res.getDimension (R.dimen.cell_height));
                // Use string resources to set the content of the text box
                text.setText (texts [position]);
                TypedArray icons = res.obtainTypedArray (R.array.plain_arr);
                // Use color resources to set the background color of the text box
                text.setBackgroundDrawable (icons.getDrawable (position));
                text.setTextSize (20);
                return text;
            }
        };
        GridView grid = (GridView) findViewById (R.id.grid01);
        grid.setAdapter (ba);
    }
}

PS
The last thing to add is that there are Boolean and integer resources.

Integer resources:

/res/values/integer.xml
  
  
    10
    20
 
java code
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
  
public class MainActivity extends Activity {
  
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
          
        // Get Resource resource, this method is executed in Activity
        Resources resources = getResources ();
        int size_1 = resources.getInteger (R.integer.size_1);
        System.out.println (size_1);
    }
}

Boolean resources:
/res/values/bool.xml
  
  
    true
    false
  

java code:
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
  
public class MainActivity extends Activity {
  
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
          
        // Get Resource resource, this method is executed in Activity
        Resources resources = getResources ();
        boolean is_true = resources.getBoolean (R.bool.is_true);
        System.out.println (is_true);
    }
}

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.