我的应用程序是关于创建调查。为此,用户应该能够动态创建新的字段。单击按钮时在同一父级中多次膨胀视图
所以,当创建一个新的调查时,只显示一个活动只有2个按钮(添加和删除)。当用户单击添加按钮时,会出现一行,其中包含EditText(以便他可以编写问题),Spinner(使用户可以选择字段类型:文本,RadioButton,CheckBox等)以及另一个EditText(其中写入了不同的可用答案选项(如果适用))。
为了实现这个功能,我创建了一个包含这些Widget的XML文件,每次用户单击添加按钮时,XML都会被夸大。这是我第一次按下按钮,但在那之后......没有任何反应。
我读了一个地方,你不能在同一个孩子中膨胀同一个孩子2次,除非它是在循环中完成的。
所以,我的问题是:
- >我怎样才能解决这个问题,并取得了预期的效果?
- >为什么这不适用于循环?
任何帮助或暗示表示赞赏。再一次,非常感谢社区给予我的所有支持。
注意:在android文档中,他们讲的是关于克隆一个充气机的信息,但缺少这方面的信息,我找不到任何其他信息。
源代码:
XML - 主要cointainer:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/mds_new_addfield_button"
android:tag="new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Field"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
/>
<Button
android:id="@+id/mds_new_deletefield_button"
android:tag="delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Field"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
XML -Inflated:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:id="@+id/mdsnew_field_tv"
style="@style/dsn_field"
android:text="Field Name "
/>
<TextView
android:id="@+id/mdsnew_type_tv"
style="@style/dsn_field"
android:text="Type of Field "
/>
<TextView
android:id="@+id/mdsnew_choices_tv"
style="@style/dsn_field"
android:text="Choices"
/>
</TableRow>
<TableRow>
<EditText
android:id="@+id/mdsnew_fieldname_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Field Name"
android:textSize="18sp"
/>
<Spinner
android:id="@+id/mdsnew_type_sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/mdsnew_choices_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choices "
android:textSize="18sp"
/>
</TableRow>
</TableLayout>
添加按钮:
LinearLayout myRoot = (LinearLayout) findViewById(R.id.wrapper);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.mds_new_row, myRoot);
您可以给同一个孩子两次或更多次充气。你不能做的就是多次使用同一个视图的实例。每次你调用`LayoutInflater`的`inflate`方法时,你都会创建一个新的`View`,所以你不应该对你做事的方式有任何问题。所以...可能是错误是其他地方...你能分享XML文件(容器和运行时膨胀的)吗?此外,你膨胀视图的代码片段将会很有帮助。 – Cristian 2010-11-29 00:46:09
编辑帖子,包括你问的问题! (感谢您的帮助!) – Tivie 2010-11-29 00:55:59