在 Android 中构建/使用运行时生成的布局 XML [英] Building/using runtime generated layout XML in Android

查看:22
本文介绍了在 Android 中构建/使用运行时生成的布局 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一个项目,该项目要求我使用 XML 文档在 Android 设备上呈现表单.必须在运行时获取并显示该表单.我想知道是否有办法标记表单 XML,使用 XSLT 将其转换为 Android 布局 XML,然后让设备呈现它.

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.

推荐答案

不幸的是,你不能仅仅克隆 LayoutInflater 或使用其他类似的技巧来做到这一点——布局膨胀完全依赖于采用 AttributeSet 参数的视图构造函数,它们完全依赖于 Context.obtainStyledAttributes 方法,而该方法本身完全依赖于具有预处理的二进制 XML 文件,以便能够进行合理有效的属性解析.

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.

您可以探索的另一种方法是在您的服务器上使用 aapt 工具(或更可能是它的黑客版本),将您生成的布局编译成适当的数据.不幸的是,我们目前没有办法从原始二进制 blob 构建 XmlPullParser(它必须从 AssetManager 获取这个 blob),因此使用这种方法在客户端和服务器上都需要做大量的工作.我怀疑人们可以想出一些非常简洁的东西,但这需要大量的工作.

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.

这篇关于在 Android 中构建/使用运行时生成的布局 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆