单击按钮时,在同一父级中多次膨胀视图

Tiv*_*vie 5 android listview inflate

我的应用程序是关于创建调查.为此,用户应该能够动态创建新字段.

因此,在创建新调查时,只显示2个按钮(添加和删除).当用户单击添加按钮时,会出现一行包含EditText(因此他可以编写问题),一个Spinner(允许用户选择字段类型:text,RadioButton,CheckBox等)和另一个EditText(其中写入了不同的可用答案选项(如果适用).

为了实现这一点,我创建了一个包含这些Widgets的XML文件,每次用户点击添加按钮时,XML都会膨胀.它是第一次按下按钮时工作,但在那之后......没有任何反应.

我读到某个地方你不能在父母身上给同一个孩子充气2次,除非它是在循环中完成的.

所以,我的问题是:

- >如何绕过这个并获得所需的效果?

- >为什么这不适用于循环?

任何帮助或提示表示赞赏.再一次,非常感谢社区给予我的所有支持.

注意:在Android文档中,他们谈到克隆一个充气机,但信息很少,我找不到任何其他信息.

源代码:

XML - 主要投标人:

<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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

添加按钮:

LinearLayout myRoot = (LinearLayout) findViewById(R.id.wrapper);

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

inflater.inflate(R.layout.mds_new_row, myRoot);
Run Code Online (Sandbox Code Playgroud)

xte*_*ore 13

您的父级是一个LinearLayout,看起来您想要一个垂直方向,但您没有指定它.尝试设置方向,看看是否有帮助.