How to add a copy of XML dynamically

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

    You are probably getting an error message that the View you are trying to add already has a parent. You would need to call removeView() on the parent of your TextViews or removeAllViews() to just remove them all before adding them to your new RelativeLayout. Something like

    m.removeAllViews();
    
    m2.addView(et1);
    m2.addView(et2);
    m2.addView(et3);
    m2.addView(et4);
    

    However, if you are adding them multiple times then you probably want to create new instances of them as you do with your RelativeLayout (m2). Then you can use the params that your original TextViews have. If this isn't your current error then please post your logcat but this will cause an exception.

    Edit

    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);
    
    //Create the new TextViews
    TextView tv1 = new TextView(m.getContext());  //if inside Activity you can use this instead of m.getContext()
    // set params which you can get from the above TextViews
    m2.addView(tv1);
    ...
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-01-17 06:24

    I FOR SURE got it working this time! I used this code:

     LinearLayout item = (LinearLayout)findViewById(R.id.tileContainerME);
            View child = getLayoutInflater().inflate(R.layout.activity_main, null);
            item.addView(child);
    

    It would copy the 'activity_main' class which holds solely the XML data for the tile. Then it copies it into the Horizontal Scroll View! Here is a screenshot of my solution:

    http://i.stack.imgur.com/WxoJG.png

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题