【问题标题】:Adding Layout dynamically for number of times on button click在按钮单击时动态添加布局次数
【发布时间】:2018-05-15 05:11:52
【问题描述】:

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

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

Buttononclicklistenercode 是,

    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: The specified child has a parent。您必须先在孩子的父级上调用 removeView()。 在我的代码中,在添加该布局之前,我已经为父级调用了方法 removeViewAt()

任何人都可以帮助我知道其中有什么问题。还有其他方法可以做到这一点吗?提前致谢!

【问题讨论】:

  • 您到底想要什么,您是要删除以前的布局,然后替换新的布局,还是只想在单击按钮时添加布​​局?
  • 我只想在单击按钮时向该布局添加一个视图(addinglayout)
  • 试试这个link,可能会满足您的要求。
  • 在您的 for 循环中,您将添加“addviewcount”乘以“待添加”。这就是您收到此错误的原因。首先,您还必须在循环中调用 removeview
  • 在 for 之外调用 removeView 是没用的。它抛出异常是因为考虑到我在 for 循环中是 '0' 它将使用“addinglayout.addView(tobeadded);”添加视图现在当我增加到“1”时,它会尝试再次添加视图,但是在“0”处添加的视图呢(当我为“0”时)。

标签: android android-layout dynamic


【解决方案1】:

您将收到 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 被附加到addinglayout。

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

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

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

【讨论】:

  • 是的,创建一个视图对象数组并将它们附加到循环中的布局有效。谢谢
【解决方案2】:
 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);

【讨论】:

    【解决方案3】:
    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);
    
    
            }
        });
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 2019-03-12
      • 2016-10-19
      • 1970-01-01
      相关资源
      最近更新 更多