Add multiple custom views to layout programmatically

后端 未结 3 1985
無奈伤痛
無奈伤痛 2020-12-24 06:30

If I for example have empty layout like this:

layout1.xml



        
3条回答
  •  醉酒成梦
    2020-12-24 06:43

    Try this method

    Add id to LinearLayout 'layout1.xml':

    
    
    
    

    In onCreate:

    LinearLayout firstlayout = (LinearLayout) findViewById(R.id.firstlayout);
    
    LinearLayout secondlayoout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.layout2, null); // inflating view from xml
    TextView btn1 = (TextView) secondlayoout.findViewById(R.id.button1);
    btn1.setText("TEST");
    
    firstlayout.addView(secondlayoout);
    

提交回复
热议问题