分享

Building/using runtime generated layout XML in Android

 quasiceo 2015-07-02

I am currently working on a project which requires me to use an XML document to render a form on an Android device. The form must be fetched and displayed at run-time. I am wondering if there is a way to tag the form XML, transform it using XSLT into an Android layout XML, and then have the device render it.

asked Dec 21 '09 at 19:30
illvm
858923

1  
Just be warned. Android does a lot of work to optimize reading and parsing of those files at run-time. So if you roll your own, be prepared for it to inflate a LOT slower. –  haseman Dec 21 '09 at 20:30

3 Answers

Unfortunately you can't just clone LayoutInflater or use other such tricks to do this -- layout inflation is entirely dependent on the view constructors which take an AttributeSet argument, which are entirely dependent on the Context.obtainStyledAttributes method, which itself is entirely dependent on having a pre-processed binary XML file to be able to do reasonably efficient attribute resolution.

An alternative approach you can explore is to use the aapt tool (or more likely a hacked version of it) on your server, to compile the layouts you generate into the appropriate data. Unfortunately we don't currently have a way to contruct an XmlPullParser from a raw binary blob (it must get this blob from the AssetManager), so there is a fair amount of work to do on both the client and server with this approach. I suspect one can come up with something pretty neat, but it will be lots of work.

answered Dec 21 '09 at 22:04
hackbod
61.9k1297135

    
Considering this is something that needs to be prototyped over the next week and a half I'm thinking that this is not an approach I should investigate until a later date. Would you agree with this? I've done building of UIs from XML at run-time before for desktop and web platforms. We also have an implementation for this for a Windows Mobile application. I might just have to mimic those approaches and investigate aapt later when I have some more time or if/when performance issues arise. –  illvm Dec 21 '09 at 22:18
    
Yeah this is not a one week project. :) The easiest route would be to just make a simple XML structure that defines what you need for your layouts, without trying to allow for every possible thing an Android layout can do. Parsing that and turning it into the construction of View layout should be fairly simple; you can add new facilities to your XML format as needed. –  hackbod Dec 21 '09 at 22:25

Android only contains a built-in way to "inflate" layout XML stored as a layout resource in the APK file. If you want to "inflate" similar (or different) XML from other sources, you will have to implement that yourself, perhaps by cloning some logic from the LayoutInflater class.

answered Dec 21 '09 at 19:53
CommonsWare
479k5010781136

The LayoutInflater.inflate method will take an XmlPullParser as an argument so all you need to do is load your dynamically created XML into a PullParser and pass that into the inflater.

answered Dec 21 '09 at 20:10
CaseyB
16.8k64682

6  
But if you actually read the documentation for that method it says "it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime". –  Mirko N. Dec 21 '09 at 20:23
    
Well that sucks, I can see that being a pretty neat feature. –  CaseyB Dec 21 '09 at 20:36

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多