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