[Android] defines drawable arrays and id arrays in xml files, and androiddrawable

Source: Internet
Author: User

[Android] defines drawable arrays and id arrays in xml files, and androiddrawable

If we need to set a group of image resources for a group of views in the Code (for example, when setting a group of corresponding images for all items in listview/gridview ), we can define a set of drawable arrays in xml that represent images and then read them using code.

At first, I did 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>

Then try to call getResources (). getIntArray in the code to obtain the image id. You can find that it does not work at all. Finally, stackoverflow provides a solution:

1. in xml, we need to define it as follows:

<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, obtain the following information:

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 image id is saved in resIds, and then we can reference it (for example, using img. setImageResourse (resIds [0]) to set the image.


Similarly, to define an id array in xml, the same approach is used.



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



How can God obtain the resource file id in xml?

Res ID is generated after compilation. It is estimated that it can only be referenced in java. You can refer to the image in the following java code (numbers 0 ~ 9. Private static final int [] resDigit = new int [] {R. drawable. number_0, R. drawable. number_1, R. drawable. number_2, R. drawable. number_3, R. drawable. number_4, R. drawable. number_5, R. drawable. number_6, R. drawable. number_7, R. drawable. number_8, R. drawable. number_9}; public static int getDigitImage (int num) {if (num> = 0 & num <resDigit. length) {return resDigit [num];} else {return resDigit [0];}
 
Android xml resource array: the content of the xml file is as follows. How can I obtain the value of the attribute name in the item key-value pair?

Read data code: getValue (this, "fourth ");

Public String getValue (Context context, String strText ){
String strData = "";
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
Document document = null;
InputStream inputStream = null;
// First find the xml file
Factory = DocumentBuilderFactory. newInstance ();
Try {
// Locate xml and load the document
Builder = factory. newDocumentBuilder ();
InputStream = context. getResources (). getAssets ()
. Open ("data. xml ");
Document = builder. parse (inputStream );
// Find the root Element
Element root = document. getDocumentElement ();
NodeList nodes = root. getElementsByTagName ("item ");
// Traverse all the child nodes of the Root Node
Element cardElement;
String strName;
String strValues;
For (int I = 0; I <nodes. getLength (); I ++ ){
CardElement = (Element) (nodes. item (I ));
StrName = cardElement. getAttribute ("value ");
Element eValues = (Element) cardElement
... The remaining full text>

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.