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

时间:2010-11-29 00:11:42

标签: android listview inflate

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

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

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

我读到某个地方,除非是在循环中完成,否则你不能在父母中为同一个孩子充气2次。

所以,我的问题是:

- >我怎样才能解决这个问题并获得预期的效果?

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

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

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

SOURce代码:

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>

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);

1 个答案:

答案 0 :(得分:13)

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