在运行时创建、存储和扩充自定义布局
我正在尝试找到以下问题的解决方案。我正在开发的应用程序需要用户能够通过自定义 UI 生成器生成自定义 UI(简单小部件的布局)。因此,用户可以在画布上堆叠小部件(主要是图像。但也包括 TextView 和 EditText)、移动它们等。 UI 必须存储在数据库中以供将来使用。因此,应该有某种机制来加载和扩展此 UI。这是主要问题。
我的第一个想法是依赖标准的 Android 布局机制。不幸的是,LayoutInflater 使用编译为二进制形式的 XML。据我所知,不可能在运行时将 XML 字符串编译为二进制表示形式。
请问,有人有解决此类问题的经验吗?有什么建议吗?
I'm trying to find a solution for the following problem. The application I work on requires a possibility for user to produce custom UI (layout of simple widgets) via custom UI builder. So, user may stack widgets (images, mostly. But TextViews and EditText, as well) on canvas, move them around, etc.
UI have to be stored in a database for future use. So, there should be some mechanism for loading and inflating of this UI. This is the main problem.
My first idea was to rely on standard Android layout mechanism. Unfortunately, the LayoutInflater works with XML compiled into a binary form. And as far as I know, it's not possible to compile an XML string into binary representation in runtime.
Does, anybody have experience with such problem? Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 LayoutInflater 的 inflate 方法。实际上,您可以为其提供任何
XmlPullParser
作为其源,而该源又可以在给定任何Reader
的情况下构造。换句话说,您可以使用几乎任何字符流作为膨胀的 xml 源。
XmlPullParser 文档的开头为您提供了创建的基本轮廓拉解析器:
更新 - 正如
LayoutInflater
文档中所述,这不会起作用。Check out the inflate methods of LayoutInflater. You can actually give it any
XmlPullParser
to use as its source, which in turn can be constructed given anyReader
.In other words, you can use just about any character stream as your xml source for inflating.
The beginning of the XmlPullParser docs gives you the basic outline for creating the pull parser:
Update - this isn't going to work, as mentioned in the
LayoutInflater
docs.