Android various Resources detailed

Source: Internet
Author: User
Tags getcolor

1, character array
The string array resource is defined by the <string-array> tag, and several <item> tags contained in the <string-array> tag represent 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 that references the string array is as follows:
String[] Plans=getresources (). Getstringarray (R.array.planets_array);

Note: You cannot use a String data resource to set a property that receives a string, for example, Android:text, because the system will directly set the ID value of the string array resource
As text is passed to this property, you should set the properties that can receive the string array resource with a string resource.

2. Plural string

<?xml version= "1.0" encoding= "Utf-8"?>

<resources>

<plurals

Name= "Plural_name" >

<item_plurals

quantity=["Zero" | "One" | "Both" | "Few" | "Many" | "Other"]

>text_string</item>

</plurals>

</resources>

The zero language requires special handling of the number 0. (e.g. Arabic)

The one language requires special handling of numbers like 1. (such as 1 in English and most other languages; in Russian, any number ending in 1 but not ending in 11 also belongs to this type.) )

The language requires special handling of numbers like 2. (e.g. Welsh)

Few languages require special processing of smaller numbers (for example, 2, 3, 4 in Czech, or 2, 3, 4 in Polish, but not 12, 13, 14). )

Many languages require special processing of large numbers (e.g., in Maltese, ending in 11-99)

The other language does not require special processing of numbers.

Using a complex string resource, you can use the Getquantitystring method, which has two overloaded forms, which are prototyped as follows:
Public String getquantitystring (int id, int quantity) throws notfoundexception;
Public String getquantitystring (int id, int quantity, Object ... formatargs) throws notfoundexception;

Where the ID parameter represents the id,quantity parameter of a complex string resource represents a specific number, for example, 1 corresponds to the Quantity attribute value of "one", and 2 corresponds to "Two" of the quantity attribute value. The Formatargs parameter represents the parameters of a complex string resource.

A complex string resource that references a number of 1 settitle (Getresources (). Getquantitystring (
R.plurals.numberofsongsavailable, 1));
A complex string resource that references a number other than the other, and a parameter value is passed to the other resource when invoked (20)
Settitle (Getresources (). Getquantitystring (
R.plurals.numberofsongsavailable, 20, 20));

 
 3, use quotation marks in strings
     values in strings can be arbitrarily specified, but when you encounter special symbols, such as double quotes, single quotes, you need to take a special approach to processing
  If it is ('), you can enclose the entire string using the escape character (\) or using double quotation marks ("). In the case of double quotes, you can use the escape character (\) before 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, formatting strings with placeholders  
    String.Format (String,object.) method to format a string with placeholders. Therefore, you can
  use the String.Format method to format a string resource whenever you insert a placeholder in a string resource. The format method requires a placeholder with% 1,%2 、。。。 %n said. Where the nth placeholder corresponds to the n+1 parameter value of the format
  method.

string resource with Placeholder
<!--$s indicates that the placeholder requires passing in a string, $d indicates that the placeholder requires an integer followed by an integer to represent the first few placeholders--
<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. Formatting string resources with HTML tags
String resources support Some HTML tags, so you can use these HTML tags to format strings directly in a string resource.
String resources formatted with HTML tags
<string name= "Welcome" >welcome to <b>Android<b/></string>

String resources support the following HTML tags
<b>: Bold characters
<i>: Italic word
<u>: underlined text
Sometimes you need to format strings using both HTML tags and placeholders, but using the String.Format method format string ignores all HTML tags in the string. In order to be
The Format method formats characters with HTML tags and needs to process the string first using the Html.fromhtml method. For example
<string name= "Welcome" >hello,%1$s! You have $lt; <b>%2d New Messages$lt;</b></string>

Note: Because you need to use the Html.formathtml method to process strings, the "<" in the HTML tag requires "&lt;" (">" can be used directly)
Java code that uses string resources
String Textstring=string.format (Getresources (). getString (R.string.welcome), "Hello", 18,20);
Charsequence styledtext=html.fromhtml (text);
If a parameter value of format contains a special string of HTML, such as "<", "&", you can use the following code to format the value of the parameter before formatting the string using the Format method
Special characters in username that contain HTML
String Escapedusername=textutil.htmlencode () Username;
String Textstring=string.format (Getresources (). getString (R.string.welcome), escapedusername,malcount);
Charsequence styledtext=html.fromhtml (text);

6. Value Resources
All resources placed in the Res/values directory belong to the value resource (which can be stored in any XML file).
1. Integer Resources
Integer resource use <integer> tag settings, code as follows:
<integer name= "Height" >75</integer>
<integer name= "width" >75</integer>
Code used in a 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 referencing an integer resource
int Width=getresources.getinteger (r.integer.width);
2. Size Resources
A dimension resource is a resource consisting of a series of floating-point numbers that need to be defined in a resource file in the Res/values directory to define a,<dimen> label for a dimension resource.
<dimen name= "SIZE_PX" >50px</dimen>
<dimen name= "Size_in" >1.5in</dimen>
<dimen name= "SIZE_SP" >10sp</dimen>
Use Java code to get dimension resources, which return their corresponding pixel values according to the corresponding dimension units
Float size1=getresources (). Getdimension (r.dimen.size_in);

If you want to get the size directly (not converted to pixels), you can use the following code
Typedvalue outvalue=new Typedvalue ();
The 3rd parameter of the GetValue method, if true, means that the system obtains the final resource through a recursive method, even if the resource value references another resource
Getresources (). GetValue (R.dimen.size_px, Outvalue, true);
Because the GetValue method does not directly return the value of the dimension resource, it needs to be converted using the Complextofloat method
Float value=typedvalue.complextofloat (outvalue.data);
3. Color Resources
The color resource is used to specify the color value, use the <color> tag settings, and the code is as follows
<color name= "Red" > #f00 </color>
Java code Get Code
int color=getresources (). GetColor (r.color.red);
4. ID Resource
The ID resource is actually the value of the Android:id attribute, using the <item> tag settings, the code is as follows:
<item type= "id" name= "BUTTON_OK" ></item>
<item type= "id" name= "dialog_exit" ></item>
The code for referencing the ID resource in the resource file is as follows:
<button android:id= "@id/button_ok"/>
Usually when you set the Android:id property, you add a "+" between "@" and "id", and the code is as follows:
<button android:id= "@+id/button_ok"/>
The meaning of this "+" is that if the ID resource does not exist, the system will automatically create an ID resource, if the ID resource exists, the system will ignore the "+".
This strategy avoids the tedious way to define the ID resource when setting the Android:id property value.
Note: Although the "+" can be used for all the properties of an acceptable ID resource, it is recommended to use "+" only in the Android:id property value. This is because other properties need to use an ID that already exists
Resource corresponding to the control, if you use "+", when the specified ID resource does not exist is the system will automatically create an ID resource, so that although the ID resource exists, but does not correspond to any of the controls, it may
Will cause the program to throw an exception, and it will not be the program's pre-set effect. For example, the Android:layout_marginleft property of the,<relativelayout> tag must be set to a
The ID resource that has been bound to a control.

5, Integer array resource
An integer array resource is used to store a set of integers, set with the <integer-array> tag, with the following code:
<integer-array name= "Bits" >
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
</integer-array>
The Java code referencing an integer array resource is as follows:
Int[] Bits=getresources (). Getintarray (R.array.bits);

6. Type Array Resource
A type array resource can store its resources as an array, so it can also be called a Resource array resource.
<array name= "Icons" >
<item> @drawable/home</item>
</array>
<array name= "Colos" >
<item> #ffff0000 </item>
</array>

The Java code of the reference type array resource 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)





Android various Resources detailed

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.