How to add a copy of XML dynamically

后端 未结 4 1247
深忆病人
深忆病人 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:31

    Inflating the same layout inside it once again might help you achieve this.

    Try this code:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
    
        LinearLayout main_layout = (LinearLayout)findViewById(R.id.tileContainerME);
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout child = (LinearLayout) inflater.inflate(R.layout.main_layout, null);
        main_layout.addView(child, 1);
    }
    

    You'll get something like this:

    enter image description here

提交回复
热议问题