LayoutInflater Performance

纵然是瞬间 提交于 2019-12-12 01:52:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!