问题
I've copied an XML file from /assets
to my applications data folder (data/data/package.name/files/
). I'm doing this because the user will be able to modify a lot of data, and I want to save that data to the internal memory and then load it again when they restart the app. This all works well, using Root Browser I can see the XML file is properly copied to the data directory.
Now I need to inflate this XML file using a LayoutInflater
. How would I access this file? With an XmlResourceParser
or XmlPullParser
?
回答1:
You cannot inflate that file using LayoutInflater
. First, you can only inflate layout resources, not arbitrary files. Second, based on the description in your first paragraph, it is not a layout file in the first place.
If you want to parse arbitrary XML, use the DOM, SAX, or XmlPullParser
.
回答2:
From the Android view API:
it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
A solution might be to pre-process XML files before. You must compress the XML file in the same way the Android SDK does at build time:
view inflation relies heavily on pre-processing of XML files that is done at build time
I guess you will have to call XmlPullParser.setInput
to load your pre-processed XML, and then pass the XmlPullParser to View.inflate
. I hope it is possible to do this kind of stuff even for content that is not in the APK.
I am very interested in any solution you can find. Please let me know if you find or create a prototype! This would be a decisive step towards creating a plugin architecture for my Open Source app.
来源:https://stackoverflow.com/questions/8197844/inflate-xml-file-from-internal-memory