开发者

Deserialize Android Layout XML?

开发者 https://www.devze.com 2023-03-15 21:55 出处:网络
Whats the best way for me to take an android XML layout file and automatic开发者_运维百科ally generate the equivalent Java code? Does a tool like JAXB work and if so how do I use it?It\'s an interesti

Whats the best way for me to take an android XML layout file and automatic开发者_运维百科ally generate the equivalent Java code? Does a tool like JAXB work and if so how do I use it?


It's an interesting idea, though a bit useless IMHO. Why would one want to do it? Inflating an XML is very fast operation and hides a lot of complexity - for example it takes into account what is the current DisplayMetrics and recalculates layout parameters (width/height) appropriately to the density and size of the screen... It's also very fast because it does not actually require XML parsing - parsing is done at compilation time and what is actually stored by android is a binary version of the layout which is optimized for efficiency (that's why you cannot build layout XML dynamically from an XML file).

If you would like to modify the Java code and add/remove some elements then it is much more efficient to inflate the XML and then do all the modifications -less clutter simpler code and all the calculations are done for you...


Whats the best way for me to take an android XML layout file and automatically generate the equivalent Java code?

Step #1: Parse the XML.

Step #2: Generate the Java code. You'll have to pray that you can build your own AttributeSet implementation that works with your generated code, otherwise this will be a very complex problem.

Does a tool like JAXB work

AFAIK, JAXB needs an XML Schema, and there is no such schema possible for the Android layout file.

Whatever problem you're trying to solve this way, there's probably a better solution.

I just want a simple way to take an android xml layout and pass it to, say, a command line tool to generate the equivalent Java.

I seriously doubt that this will be "simple" for any reasonable definition of the term.

0

精彩评论

暂无评论...
验证码 换一张
取 消