充气视图在同一个父多次点击一个按钮时 [英] Inflating a view multiple times in the same parent when a button is clicked

查看:136
本文介绍了充气视图在同一个父多次点击一个按钮时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是如何创建的调查。为了做到这一点,用户应能够动态地创建新的领域。

所以,创造一个新的调查时,一个活动显示,只有2个按钮(添加和删除)。当用户点击添加按钮,一出现一行包含的EditText(这样他就可以写的问题),一个微调(使用户能够选择的字段类型:文本,单选按钮,复选框,等..),另一个的EditText(在不同的可用答案选项都写,如果适用)。

要做到这一点,我创建了一个包含这些窗口小部件和每个用户单击Add按钮时一个XML文件,XML被夸大。它的工作原理我第一次preSS的按钮,但是......没有任何反应后。

我读的地方,你不能夸大相同的孩子2次在父,除非它是在一个循环中完成的。

所以,我的问题是:

- 我>如何解决这个问题,并取得了预期效果。

- >为什么不这样适用于一个循环

任何帮助或暗示的AP preciated。再次,非常感谢你对所有的社区给予我的支持。

注:在他们谈论克隆充气但信息有稀缺的,我找不到任何关于它的其他信息在Android文档

源$ C ​​$ C:

XML - 主要cointainer:

 < RelativeLayout的
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    <的LinearLayout
        机器人:ID =@ + ID /包装
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
    />

    <按钮
        机器人:ID =@ + ID / mds_new_addfield_button
        机器人:标签=新
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=添加域
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentLeft =真
    />
    <按钮
        机器人:ID =@ + ID / mds_new_deletefield_button
        机器人:标签=删除
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=删除字段
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentRight =真
    />

< / RelativeLayout的>
 

XML -Inflated:

 < TableLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
>
    <的TableRow>
        <的TextView
            机器人:ID =@ + ID / mdsnew_field_tv
            风格=@风格/ dsn_field
            机器人:文本=字段名
        />

        <的TextView
            机器人:ID =@ + ID / mdsnew_type_tv
            风格=@风格/ dsn_field
            机器人:文本=字段类型
        />

        <的TextView
            机器人:ID =@ + ID / mdsnew_choices_tv
            风格=@风格/ dsn_field
            机器人:文本=选择
        />
    < /的TableRow>

    <的TableRow>

        <的EditText
            机器人:ID =@ + ID / mdsnew_fieldname_et
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=字段名
            机器人:TEXTSIZE =18sp
        />

        <微调
            机器人:ID =@ + ID / mdsnew_type_sp
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
        />

        <的EditText
            机器人:ID =@ + ID / mdsnew_choices_et
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=选择
            机器人:TEXTSIZE =18sp
        />

    < /的TableRow>
< / TableLayout>
 

添加按钮:

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

LayoutInflater充气=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

inflater.inflate(R.layout.mds_new_row,myRoot);
 

解决方案

您的父母是的LinearLayout,它看起来像你预期的垂直方向,但你没有指定这一点。尝试设置方向,看看有没有什么帮助。

My app is about creating surveys. In order to do so, the user should be able to create new fields dynamically.

So, when creating a new survey, an Activity is shown with only 2 buttons (add and delete). When a user clicks the add button, a row appears containing a EditText(so he can write the question), a Spinner(that enables user to select the type of field: text, RadioButton, CheckBox, etc..) and another EditText (in which the different available answer choices are written, if applicable).

To accomplish this I created a XML file containing these Widgets and each time the user clicks in the add button, the XML is inflated. It works the first time I press the button, but after that... nothing happens.

I read somewhere that you cannot inflate the same child 2 times in a parent unless it is done in a loop.

So, my questions are:

-> How can I get around this and obtain the desired effect?

-> Why doesn't this apply in a Loop?

Any help or hint is appreciated. Once again, thank you very much for all the support the community has given me.

NOTE: In the android documentation they speak about cloning an inflator but the information there is scarce and I couldn't find any other info about it.

SOurce code:

XML - Main 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>

Add Button:

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

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

inflater.inflate(R.layout.mds_new_row, myRoot);

解决方案

Your parent is a LinearLayout, and it looks like you intended a vertical orientation, but you haven't specified this. Try setting the orientation and see if that helps.

这篇关于充气视图在同一个父多次点击一个按钮时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆