按钮单击时动态添加布局次数

时间:2017-12-01 06:10:35

标签: android android-layout dynamic

  

场景是,当用户点击按钮时,我需要添加   在单击按钮的次数的父布局的已知位置的特定布局。

我不知道它是否有效。我尝试了以下解决方案,我从其他帖子

按钮onclicklistener代码为,

    parent = (ViewGroup) C.getParent();//c is a layout in which position i want to add view
    final int index = parent.indexOfChild(C);
    tobeadded=getLayoutInflater().inflate(R.layout.block_tobeadded_foremi,null);
    ((Button)findViewById(R.id.button88)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addviewcount+=1;
            LinearLayout addinglayout=new LinearLayout(MyActivity.this);
            addinglayout.setOrientation(LinearLayout.VERTICAL);
            parent.removeViewAt(index);
            addinglayout.removeAllViews();
            for(int i=0;i<addviewcount;i++)
                   addinglayout.addView(tobeadded);
            parent.addView(addinglayout, index);
        }
    });

但是我得到 java.lang.IllegalStateException:指定的子节点已经有父节点。您必须先在子级父级上调用removeView()。在我的代码中,在添加该布局之前,我已为父级调用方法removeViewAt()

任何人都可以帮助我知道那里有什么问题。还有其他办法吗?提前谢谢!

3 个答案:

答案 0 :(得分:2)

您收到IllegalStateException,因为您已将子项附加到布局的根目录。

parent.removeViewAt(index);
addinglayout.removeAllViews();
for(int i=0;i<addviewcount;i++)
     addinglayout.addView(tobeadded);//Exception in this line
parent.addView(addinglayout, index);

我们假设addviewcount是2.

现在在第一次迭代中,tobeadded附加到addslayout。

在第二次迭代中,您再次尝试附加到addlayout,这会导致异常,因为指定的子项已经有父。要解决此问题,您必须首先在孩子的父母上调用removeView()

总而言之,没有孩子应该有多个父母。而你实现你想要做的事情的方式是错误的。创建一个View对象数组,并将它们附加到循环中的布局中。这将解决您的问题。

这是一个链接,我几个月前就已经详细回答了类似的问题。 https://stackoverflow.com/a/46672959/2356570

答案 1 :(得分:1)

 LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                              LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

点击按钮试试这个:

addinglayout.removeAllViews();
View text = new View(context);
textNotes.setLayoutParams(lparams);
addinglayout.addView(text);

答案 2 :(得分:1)

public class SampleDynamiclay extends AppCompatActivity {

Button add, replace;
LinearLayout dynamiclay;
int newid = 100;
int replaceid;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sampledynamiclay);

    add = (Button) this.findViewById(R.id.add);
    replace = (Button) this.findViewById(R.id.replace);
    dynamiclay = (LinearLayout) this.findViewById(R.id.dynamiclay);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView txt = new TextView(SampleDynamiclay.this);
            txt.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            newid = newid + 1;
            txt.setTag(newid + "");
            txt.setTextColor(getResources().getColor(R.color.black));
            txt.setText("TextView  " + newid);
            dynamiclay.addView(txt);
        }
    });

    replace.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            newid = newid + 1;
            View removableView = dynamiclay.getChildAt(2);
            dynamiclay.removeView(removableView);
            TextView txt = new TextView(SampleDynamiclay.this);
            txt.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            txt.setTag(newid + "");
            txt.setTextColor(getResources().getColor(R.color.black));
            txt.setText("TextView is replaced " + newid);
            dynamiclay.addView(txt, 2);


        }
    });

}

}

response screenshot in mobile of above code

相关问题