Android resources and android Resources
1. character array
The string array resource is defined by the <string-array> tag. Several <item> tags contained in the <string-array> tag indicate the array elements.
For example
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name = "planets_array">
<Item> Mercury </item>
<Item> Values </item>
<Item> Earth </item>
<Item> Mars </item>
</String-array>
</Resources>
The code for referencing a string array in the Code is as follows:
String [] plans = getResources (). getStringArray (R. array. planets_array );
Note: you cannot use string data resources to set the properties of the receiving string, for example, android: text, because the system will directly set the ID value of the string array Resource
To pass the attribute as text, you should use string resources to set the attributes that can receive String Array resources.
2. Plural string
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Plurals
Name = "plural_name">
<Item_plurals
Quantity = ["zero" | "one" | "two" | "few" | "other" | "other"]
> Text_string </item>
</Plurals>
</Resources>
The zero language requires special processing on numbers 0. (For example, Arabic)
One language requires special processing of numbers similar to 1. (For example, 1 in English and most other languages; in Russian, any number ending with 1 but not ending with 11 also belongs to this type .)
The two language requires special processing for numbers similar to 2. (For example, Jianyu)
The few language requires special processing of small numbers (for example, 2, 3, and 4 in the Czech language; or the number ending with 2, 3, and 4 in Polish but not 12, 13, or 14 .)
The upper language requires special processing of large numbers (for example, the number ending with 11-99 in Malta)
The other language does not require special processing of numbers.
You can use the getQuantityString method to use the plural string resources. This method has two reload forms. Their prototype is as follows:
Public String getQuantityString (int id, int quantity) throws NotFoundException;
Public String getQuantityString (int id, int quantity, Object... formatArgs) throws NotFoundException;
The id parameter indicates the ID of the plural string resource, and the quantity parameter indicates the specific number. For example, 1 corresponds to "one" for the quantity attribute value, and 2 corresponds to "two" for the quantity attribute value ". The formatArgs parameter indicates the parameters of the plural string resource.
// Reference the plural string resource setTitle (getResources (). getQuantityString (
R. plurals. numberOfSongsAvailable, 1 ));
// The resource that references the plural string whose number is other. A parameter value (20) is passed to the other resource during the call)
SetTitle (getResources (). getQuantityString (
R. plurals. numberOfSongsAvailable, 20, 20 ));
3. Use quotation marks in strings
Although the value in a string can be specified at will, special methods are needed to deal with special characters, such as double quotation marks and single quotation marks.
If it is ('), you can use the Escape Character (\) or double quotation marks (") to enclose the entire string. For double quotation marks, you can use the Escape Character (\) before the double quotation marks (\). For example:
<! -- Output This'll work -->
<String name = "str1"> "This'll work" </string>
<! -- Output This'll also work -->
<String name = "str2"> This \'ll also work </string>
<! -- Output "apple" -->
<String name = "str3"> \ "apple \" </string>
4. format the string with a placeholder
The String. format (String, Object...) method can be used to format strings with placeholders. Therefore, you only need to insert placeholders in string resources.
Is To format String resources using the String. format method. The format method requires the placeholder to use % 1, % 2 ,... % N. The Nth placeholder is in format.
The n + 1 parameter value of the method corresponds.
String resources with placeholders
<! -- $ S indicates that the placeholder must be a string, and $ d indicates that the placeholder must be an integer after the integer % to indicate the placeholder number. -->
<String name = "welcome"> hello, % 1 $ s! Youhanv % 2 $ d/% 3 $ d new messages </string>
Java code for formatting string Resources
String textString = String. format (getResources (). getString (R. string. welcome), "hello", 18, 20 );
5. Format String resources with HTML tags
String resources support some HTML tags. Therefore, you can use these HTML tags to format strings directly in string resources.
String resources formatted with HTMl tags
<String name = "welcome"> Welcome to <B> Android <B/> </string>
String resources support the following HTML tags
<B>: bold
<I>: italics
<U>: Underlined text
Sometimes HTML tags and placeholders must be used to format strings at the same time. However, all HTML tags in strings are ignored when String. format is used. To
The format method can format characters with HTMl tags. You need to use the Html. fromHTML method to process the strings first. For example
<String name = "welcome"> hello, % 1 $ s! You have $ lt; <B> % 2d new messages $ lt; </B> </string>
Note: Because the Html. formatHTML method is required to process strings, "<" needs to be used in the HTMl Tag (">" can be used directly)
Use the Java code of string Resources
String textString = String. format (getResources (). getString (R. string. welcome), "hello", 18, 20 );
CharSequence styledText = Html. fromHtml (text );
If a parameter value in format contains a special HTML string, such as "<", "&", you can use the following code to format the parameter value first.
// Username contains special HTML characters
String escapedUsername=TextUtil.html Encode () username;
String textString = String. format (getResources (). getString (R. string. welcome), escapedUsername, malCount );
CharSequence styledText = Html. fromHtml (text );
6. value resources
All resources in the res/values directory belong to value resources (which can be stored in any XML file ).
1. Integer Resources
Use the <integer> label to set integer resources. The Code is as follows:
<Integer name = "height"> 75 </integer>
<Integer name = "width"> 75 </integer>
Code Used in the resource file
<TextView
Android: id = "@ + id/text"
Android: layout_width = "@ integer/height"
Android: layout_height = "@ integer/width"
Android: layout_alignParentBottom = "true"
Android: text = "@ string/str3"
/>
The following is the java code that references an integer resource.
Int width = getResources. getInteger (R. integer. width );
2. Size Resources
Size resources are Resources composed of a series of floating point numbers. These resources need to be defined in the resource file of the res/values directory. The <dimen> label is used to define size resources.
<Dimen name = "size_px"> 50px </dimen>
<Dimen name = "size_in"> 1.5in </dimen>
<Dimen name = "size_sp"> 10sp </dimen>
Use java code to obtain the size resource. These methods return the corresponding pixel value based on the corresponding size unit.
Float size1 = getResources (). getDimension (R. dimen. size_in );
If you want to directly obtain the size (not converted to pixels), you can use the following code
TypedValue outValue = new TypedValue ();
// If the first parameter of the getValue method is true, it indicates that even if the resource value references another resource, the system will obtain the final resource through recursive method.
GetResources (). getValue (R. dimen. size_px, outValue, true );
// Because the getValue method does not directly return the size resource value, you need to use the complexToFloat Method for conversion.
Float value = TypedValue. complexToFloat (outValue. data );
3. Color Resources
The color resource is used to specify the color value and set it using the <color> label. The Code is as follows:
<Color name = "red" ># f00 </color>
Java code
Int color = getResources (). getColor (R. color. red );
4. ID Resource
The ID resource is actually the value of the android: id attribute. Use the <item> label to set it. The Code is as follows:
<Item type = "id" name = "button_ OK"> </item>
<Item type = "id" name = "dialog_exit"> </item>
The code for referencing an ID resource in a resource file is as follows:
<Button android: id = "@ id/button_ OK"/>
When setting the android: id attribute, a "+" is added between "@" and "id". The Code is as follows:
<Button android: id = "@ + id/button_ OK"/>
This "+" indicates that if the ID resource does not exist, the system automatically creates an ID resource. If the ID resource exists, the system ignores "+ ".
This policy avoids the tedious way to define id resources when setting android: ID attribute values.
Note: Although "+" can be used for all attributes that accept ID resources, we recommend that you use "+" only for the android: id attribute value ". this is because the existing ID must be used for other attributes.
The control corresponding to the resource. If "+" is used, the system automatically creates an ID resource when the specified ID resource does not exist. In this way, although the ID resource exists, it does not correspond to any control, possible
Will throw an exception to the program, and it will not achieve the preset effect. For example, the android: layout_marginLeft attribute of the <RelativeLayout> tag must be set
ID resource bound to a control.
5. Integer array Resources
The integer array resource is used to store a group of integers and is set with the <integer-array> label. The Code is as follows:
<Integer-array name = "bits">
<Item> 4 </item>
<Item> 5 </item>
<Item> 6 </item>
<Item> 7 </item>
</Integer-array>
The java code for referencing an integer array resource is as follows:
Int [] bits = getResources (). getIntArray (R. array. bits );
6. type array Resources
A resource of the type array can store resources as an array, which is also called a resource array resource.
<Array name = "icons">
<Item> @ drawable/home </item>
</Array>
<Array name = "colos">
<Item> # ffff0000 </item>
</Array>
The java code for referencing array resources is as follows:
TypeArray drawable = getResources (). obtainTypedArray (R. array. icons );
Drawable drawable = icons. getDrawable (0 );
TypeArray color = getResources (). obtainTypedArray (R. array. colos );
Int color = color. getColor (0, 0)