【发布时间】:2014-04-22 14:59:48
【问题描述】:
我正在尝试实现下面的布局,其想法是对动态 UI 进行编程:
https://www.dropbox.com/s/stlirgv7coqf0eu/mockup1.png.
我用于应用程序的JSON如下:
{
"name": "Formulare",
"url": "http://server.save-data.de/13467",
"rows": 5,
"cols": 3,
"formItems": [
{
"type": "button",
"value": "Abschicken",
"action": "send",
"posX": 2,
"posY": 4,
"width": 2,
"height": 2
},
{
"type": "label",
"value": "Vorname",
"posX": 1,
"posY": 2,
"width": 2,
"height": 2
},
{
"type": "label",
"value": "Nachname",
"posX": 1,
"posY": 3,
"width": 2,
"height": 2
},
{
"type": "inputFieldText",
"value": "Max",
"validation": "50",
"placeholder": "Vorname",
"name": "firstname",
"localStorage": true,
"mandatory": true,
"posX": 2,
"posY": 2,
"width": 2,
"height": 3
},
{
"type": "inputFieldText",
"value": "Mustermann",
"validation": "50",
"placeholder": "Nachname",
"name": "lastname",
"localStorage": true,
"mandatory": true,
"posX": 2,
"posY": 3,
"width": 2,
"height": 3
}
]}
我使用模拟 http://www.mocky.io/v2/5316e43f2b7484ac02382f7c 作为 JSON-Data 和我从 FasterXML Jackson 中选择的 JSON-Parser -> (github.com/FasterXML/jackson)
实现 JSON 到 Java 对象的工作!问题是,我怎样才能定位对象? 我猜 GridLayout 适合我的需求,但我不知道没有 XML 是如何实现的。
我只能在列表中显示对象,这里是代码:
ScrollView scrollView = (ScrollView) findViewById(R.id.formContainer);
GridLayout layout = new GridLayout(getApplicationContext());
layout.setOrientation(GridLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
List<FormItem> formItems = form.getFormItems();
if (formItems != null) {
for (FormItem formItem : formItems) {
layout.addView(formItem.getViewObject(this));
}
}
scrollView.addView(layout);
【问题讨论】:
-
您可以设计一个布局项目,该布局项目在水平方向的线性布局中具有 textview/edittext 、复选框和 radiogroup。 \n 为列表中的每个新条目填充此布局
-
@BlackBeard 你能给我看一个LinearLayout的示例代码吗?你是什么意思?
-
您遇到的错误是什么?你的getViewObject()方法有什么用??
-
项目实际上显示在列表中,因为 List
formItems = form.getFormItems(); -> dropbox.com/s/6jtnom1zyoetqqs/… 但我想通过在 JSON-Data 中定义来显示项目的位置。目标是以这种形式显示 UI:-> dropbox.com/s/xzgwfdu80g2lrwg/… -
getViewObject() 方法进入工厂类并获取 JSON 数据。这是按钮的示例代码: public View getViewObject(DynamicForm form){ Button dfbutton = new Button(form); dfbutton.setText(this.getValue()); dfbutton.setWidth(this.getWidth()); dfbutton.setHeight(this.getHeight()); dfbutton.setX(this.getPosX()); dfbutton.setY(this.getPosY());返回df按钮; }
标签: java android json jackson android-gridlayout