Detailed explanations of various Android resources

Source: Internet
Author: User
Tags getcolor

1, character array
Use the string array resource <string-array> tag definition, in <string-array> include some tags <item> array element tags.
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 in code such as the following:
String[] Plans=getresources (). Getstringarray (R.array.planets_array);

Note: You cannot use string data resources to set properties that receive strings, such as 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 are capable of receiving string array resources with string resources.

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. (Arabic, for example)

The one language requires special treatment for numbers like 1.

(for example, 1 in English and most other languages; in Russian, no matter what ends in 1 but does not end with 11, it also belongs to this type.) )

The language requires special treatment for a number similar to 2. (for example, 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 language requires special treatment for larger numbers (for example, in Maltese, the number ending in 11-99)

Other languages do not require special processing of numbers.

Use a complex string resource to use the Getquantitystring method. There are two overloaded forms of this method. Their prototypes are as follows:
Public String getquantitystring (int id, int quantity) throws notfoundexception;
Public String getquantitystring (int id, int quantity, Object ... formatargs) throws notfoundexception;

The ID reference indicates that the id,quantity parameter of the complex string resource represents a detailed number, for example, "one" of the 1 corresponding Quantity attribute value. 2 The corresponding Quantity property value of "a". The Formatargs parameter represents the number of references to 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 reference value is passed to the other resource when called (20)
Settitle (Getresources (). Getquantitystring (
R.plurals.numberofsongsavailable, 20, 20));


3. Using an argument in a string
The value in the string, although it can be arbitrarily specified, is encountered when a special symbol. such as double-cited, single-cited, you need to take a special method to deal with
Assume (') that you can enclose the entire string using the escape character (\) or using the double-argument ("). The assumption is a double-argument, which is able to use the escape character (\) before the double-argument. Like what:
<!--output this ' ll work---
<string name= "str1" > "This's 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.

So. Simply inserting a placeholder into a string resource will allow you to
is to format the string resource with the String.Format method.

The format method requires a placeholder with% 1,% 2, and.

。。 %n said.

Where the nth placeholder is in format
The n+1 of the method corresponds to the number of references.

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. The ability to format strings directly in string resources using these HTML tags.
String resources formatted with HTML tags
<string name= "Welcome" >welcome to <b>Android<b/></string>

String resource support such as the following HTML tags
<b>: Bold characters
<i>: Italic word
<u>: underlined text
Sometimes it is necessary to format the string 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 is capable of formatting characters with HTML tags, and you need to use the Html.fromhtml method to process the strings first. Like what
<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 needs to be represented by "&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);
Assume that a certain value of format includes special strings for HTML, such as "<", "&". The following code can be used to format the parameters before formatting the string using the Format method
Special characters that include HTML in username
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 folder belong to the value resource (which can be stored in a random XML file).
1. Integer Resources
Integer resource use <integer> label settings. 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 referencing an integer resource
int Width=getresources.getinteger (r.integer.width);
2. Size Resources
A dimension resource is a resource of a series of floating-point numbers that define the,<dimen> tag in the resource file of the Res/values folder to define the size 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);

Assuming that 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 is assumed to be true, which means that, even if the resource value refers to another resource, the system obtains the last resource through a recursive method
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
Color resources are used to specify color values, using the <color> tag settings, code such as the following
<color name= "Red" > #f00 </color>
Java Code Replacement code
int color=getresources (). GetColor (r.color.red);
4. ID Resource
The ID resource is actually the value of the Android:id property, 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 referencing the ID resource in the resource file is as follows:
<button android:id= "@id/button_ok"/>
Typically, when you set the Android:id property, you add a "+" between "@" and "id", such as the following code:
<button android:id= "@+id/button_ok"/>
The meaning of this "+" is that if the ID resource does not exist, the system will proactively create an ID resource. Assuming the ID resource exists, the system ignores "+".


This strategy avoids the tedious way to define the ID resource when setting the Android:id property value.


Note: Although all acceptable ID resource properties can use "+", it is recommended to use "+" only in the Android:id property value. This is because other properties need to use an existing ID
The corresponding control for the resource, assuming that "+" is used. When the specified ID resource does not exist, the system will proactively create an ID resource, so that although the ID resource exists, it does not have a corresponding control of whatever. possible
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. With the <integer-array> tag settings, 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 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 a resource 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)





Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

A detailed explanation of various Android resources

Related Article

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.