Using Android Resources

Source: Internet
Author: User

1. What kind of resources can we name?

For:

res/anim/

XML files, which are compiled into frames by frame animation (frame by frame animation) or tweened animation (tweened animation) objects

res/drawable/

. png,. 9.png,. jpg files, which are compiled into the following drawable resource subtypes:

To get a resource of this type, you can use the resource.getdrawable (ID)

Bitmap file

9-patches (variable-size bitmap)

In order to obtain the resource type, use Mcontext.getresources (). getdrawable (R.drawable.imageid)

Note: The image resources placed here may be automatically optimized for lossless compression by the AAPT tool. For example, a true color but does not require a 256-color PNG may be converted to a 8-bit PNG with a palette. This allows images of the same quality to occupy less resources. So we have to realize that these binary images placed in this directory may change at the time of generation. If you want to read an image bit stream and convert it to a bitmap (bitmap), place the image file in the res/raw/directory so that you can avoid being automatically optimized.

res/layout/

An XML file that is compiled into a screen layout (or part of a screen). See layout declarations (declaring layouts)

res/values/

can be compiled into XML files of many types of resources.

Note :   Unlike other res/folders, it can hold any number of files that hold the description of the resource to be created, not the resource itself. The XML element type controls where these resources should be placed in the R class.

Although the files in this folder can be arbitrarily named, the following are some of the more typical files (the Convention for file naming is to include element types in the name):

       array.xml   definition array

      colors.xml  A string value that defines the color drawable and colors (color string values). Use Resource.getdrawable () and Resources.getcolor () to obtain these resources separately.

      dimens.xml defines the dimension value (dimension value). Use Resources.getdimension () to obtain these resources.

       strings.xml defines a string (string) value. Use Resources.getstring () or Resources.gettext () to obtain these resources. GetText () retains a rich text style applied on the UI string.

       styles.xml   Define style object.

res/xml/

Arbitrary XML files can be read at run time by calling Resources.getxml ().

res/raw/

Copy directly to any file in the device. They do not need to be compiled and added to the compressed file generated by your application compilation. To use these resources, you can call Resources.openrawresource (), which is the ID of the resource, which is r.raw. somefilename.

and asset.

Application syntax: R. Resource_type.resource_name

What is 2.r.java?

When compiling a resource, R.java is generated, which is used to record the ID of each resource, so that resources can be used by R.resource_type.resource_name . Except asset.

3. There is a connection between the resource reference syntax and the assignment ID for the UI control.

resource references are indexed to specific UI controls by ID, so it is convenient for Android to reference UI controls.

4. When generating the resource ID, will the extension be used?

No.

5. What happens if two resources have different extension names?

Compilation failed.

6. What is the difference between raw resources and XML resources, and asset?

Original resources: res/raw/is usually mp3,txt etc r.raw.id

XML resource : XML r.xml.id

Asset: Open directly through Assetmanager. ID not generated, same as Res

Can 7.xml resources be localized?

TBD.

Can 8.asset be localized?

No. Asset can have any subdirectory, and res can have only files below it. This should be required for R.java to generate the ID.

9. Resource reference syntax

R.resource_type.resource_name

10. Can I declare my ID in advance?

In the values newly established ids.xml, the ID is declared here:

<itemType="id"name="Right">false</item>   

It's @id, not @+id.

11. Those XML nodes can be used to create IDs

TBD.

What are the 1 and other meanings of 12.Plurals resources?

Help us to recognize the plural when used, 1 means a single, other represents 0 other, or >=2.

16. String can use HTML strings.

OK.

< string name="welcome">Welcome to < b >Android</ b >!</ string >17. How do I display HTML characters in the text?
Sometimes you might want to create a styled text resource that you can use as a format string. Usually this does not take effect,
Because String.Format (String, Object ...) Method removes all style information within the string.
The workaround is to write an escaped HTML tag and then restore the styles with fromhtml (String) after formatting. For example: (1stores the styled text resource as an escaped HTML string:<resources> <string name= "Welcome_messages" >hello,%1$s! You have &lt;b>%2$dNewMessages&lt;/b>.</string></resources>in this formatted string, add a<b> elements. Note that the left angle bracket is marked with a marker &Lt escaped html. (2then, format the string in the usual way, and call Fromhtml (string) to convert the HTML text into styled text. Resources Res=getresources (); String text=String.Format (res.getstring (r.string.welcome_messages), username,mailcount); Charsequence Styledtext=html.fromhtml (text); Because the fromhtml (string) method formats all HTML content, be sure to use HTMLEncode (string) to escape all possible HTML characters in the string with formatted text.
For example, if you want to include a possible inclusion such as "< "or" &"If the string of characters is passed as a parameter to String.Format (), the characters must be escaped before formatting.
After formatting, and then passing the string into fromhtml (string), these special characters can be restored to the original meaning. For example:String Escapedusername=Textutil.htmlencode (username) Resources Res=getresources (); String text=String.Format (res.getstring (r.string.welcome_messages), escapedusername,mailcount); Charsequence Styledtext=html.fromhtml (text);

18. How to define a drag rectangle:

Use the. 9.png format to

19. How to use

In the direction that can be dragged, you can set the length without limit.

20 How to read the XML under the title Res/xml

by Activity.getresource (). Getxml Xmlpresereader

What is around 21.assetmanager?

The main function is to read asset

22.resource can read all res files and get through the activity

23. Can I create any subdirectories under asset and

24. Can I create a subdirectory under Res/xml and not

25. What is a resource configuration modifier?

is the effect of making the UI present to points through different configurations, such as language.

Resources are used in this introduction: The next part will tell about the use of Android 4 components.

Using Android Resources

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.