创建,存储和膨胀的自定义布局在运行时自定义、布局

2023-09-06 04:09:32 作者:粗暴女友

我试图寻找以下问题的解决方案。我工作的应用程序需要通过自定义用户界面生成器用户生成自定义UI可能性(简单的部件布局)。因此,用户可以叠加在画布上的小部件(图片居多。但TextViews和EditText上,也一样),移动它们,等等。 UI必须存储以备将来使用的数据库。所以,应该有一定的机制,该UI的加载和膨胀。这是主要的问题。

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.

我的第一个想法是依靠标准Android布局机制。不幸的是,LayoutInflater处理XML编译成二进制形式。而据我所知,这是不可能的编译一个XML字符串转换成二进制重新presentation在运行。

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?

推荐答案

检查出的 LayoutInflater 。实际上,你可以给它任何 XmlPullParser 作为其源,从而可以构造给予一项使用阅读

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 any Reader.

在换句话说,你可以使用几乎所有的字符流作为您的XML源充气。

In other words, you can use just about any character stream as your xml source for inflating.

在 XmlPullParser 一开始给你基本概述了创建拉解析器:

The beginning of the XmlPullParser docs gives you the basic outline for creating the pull parser:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader("<foo>Hello World!</foo>"));

更新 - 这是行不通的,正如在 LayoutInflater 文档

Update - this isn't going to work, as mentioned in the LayoutInflater docs.

 
精彩推荐
图片推荐