问题
In Android reference, it says
For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)
Details here
if it really causes performance reasons why Android developers don't solve this problem? Is it any solution about it? Can I use a XML parser before inflate layout file?
回答1:
Android's string processing is slow. So they actually do solve it by pre-processing the xml file. When you think about it, your layout xmls should be static cause any change you made on them will most likely also cause code changes. What is the point of being able to inflate a remote layout file if you can't use it without code changes especially when the string processing is that slow.
A better solution might be, writing a class file that creates the layout you need, put it in somewhere lets say internet. Than on the runtime you can download that file, load it as a class dynamically using reflection and use it.
Check out this links for further information.
Java Reflection - Dynamic Class Loading and Reloading
How to load a Java class dynamically on android/dalvik?
来源:https://stackoverflow.com/questions/23586899/layoutinflater-performance