简体   繁体   中英

Can I add my own .xml file and use it as a resource in Android

I'm very new to Android programming, and currently trying to wrap my head around how it works. I want to have a file containing certain information I would need in my app, like an .xml file containing different time periods and info about them.

Is it possible to put that file in the resources, and read from them directly in an activity using context? Or would something like a separate .xml parser function be needed? Is there a better way to store a file/information like this?

Example of data i want to store in the xml to be read according to the period and angle:

<period name="1">
    <angle name="1">
       <box name="1">9</box>
       <box name="2">6</box>
       <box name="3">5</box>
    </angle>
</period>
<period name="2">
    <angle name="20">
       <box name="1">12</box>
       <box name="2">6</box>
       <box name="3">7</box>
    </angle>
</period>

Just create a new resource folder. If your using android studio right click on the res folder and click new -> Android Resource Directory and select resource type -> xml.

Here you can put your XML files and access them over the R.xml.YOUR_ID (this will return the ID, so to read do the following):

Resources resources = activity.getResources();
XmlResourceParser xmlResourceParser = resources.getXml(R.xml.your_YOUR_ID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM