"Android" defines drawable arrays, id arrays, and so on in an XML file

Source: Internet
Author: User

If we need to set up a set of picture resources for a set of view settings in code, such as setting a corresponding set of images for all of Listview/gridview's item, we can define a set of drawable arrays representing the pictures in the XML and then read them in code.

At first I was doing this:

<integer-array name= "Actions_images" >    <item> @drawable/pencil1</item>    <item>@ drawable/pencil2</item>    <item> @drawable/pencil3</item>    <item> @drawable/pencil4 </item>    <item> @drawable/pencil5</item>    <item> @drawable/pencil6</item> </integer-array>

And then in the code,tried toCall Getresources (). Getintarray to get the ID of the image, you can see that it doesn't work. Finally, StackOverflow gives the solution:

1, in the XML to define this:

<string-array name= "Actions_images" >    <item> @drawable/pencil1</item>    <item>@ drawable/pencil2</item>    <item> @drawable/pencil3</item>    <item> @drawable/pencil4 </item>    <item> @drawable/pencil5</item>    <item> @drawable/pencil6</item> </string-array>

2, in the Java code in this way to get:

TypedArray ar = context.getresources (). Obtaintypedarray (r.array.actions_images); int len = Ar.length ();     int[] Resids = new Int[len];     for (int i = 0; i < len; i++)         resids[i] = Ar.getresourceid (i, 0); Ar.recycle ();  

The ID of the picture is saved in the Resids, and then we can refer to it (for example, with Img.setimageresourse (resids[0)) to set the picture.


Similarly, it is the same thing to define an array of IDs in XML.



Reference: Http://stackoverflow.com/questions/6945678/android-storing-r-drawable-ids-in-xml-array


"Android" defines drawable arrays, id arrays, and so on in an XML file

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.