How to add a copy of XML dynamically

后端 未结 4 1296
深忆病人
深忆病人 2021-01-17 06:08

I am making an Android app, and I want to copy some XML code in a Linear Layout, and re-insert it into the Linear Layout so that there are two of the Relative Layouts in the

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-17 06:24

    I figured it out!

       LinearLayout m3 = (LinearLayout)findViewById(R.id.tileContainerME);
        RelativeLayout m = (RelativeLayout)findViewById(R.id.tilesAreHERE);     
        RelativeLayout m2 = new RelativeLayout(m.getContext());
    
        m2.setLayoutParams(m.getLayoutParams());
    
    
        TextView et1 = (TextView) findViewById(R.id.top1);
        TextView et2 = (TextView) findViewById(R.id.right1);
        TextView et3 = (TextView) findViewById(R.id.left1);
        TextView et4 = (TextView) findViewById(R.id.bottom1);
    
        TextView tv1 = new TextView(et1.getContext());
        TextView tv2 = new TextView(et2.getContext());
        TextView tv3 = new TextView(et3.getContext());
        TextView tv4 = new TextView(et4.getContext());
        tv1.setText(et1.getText());
        tv2.setText(et2.getText());
        tv3.setText(et3.getText());
        tv4.setText(et4.getText());
        tv1.setLayoutParams(et1.getLayoutParams());
        tv2.setLayoutParams(et2.getLayoutParams());
        tv3.setLayoutParams(et3.getLayoutParams());
        tv4.setLayoutParams(et4.getLayoutParams());
    
        m2.addView(tv1);
        m2.addView(tv2);
        m2.addView(tv3);
        m2.addView(tv4);
    
        m3.addView(m2);
    

    This will make a copy of the previous layout and then add it back into the current layout!

提交回复
热议问题