How to inflate another xml on clicking a button

前端 未结 2 2064
北恋
北恋 2021-01-25 01:45

I have one Activity in that there are no Buttons but I have coded it in such a way that another Activity comes, now in this second A

2条回答
  •  感动是毒
    2021-01-25 02:34

    I tried this code..hope helps to you..

    1.layout_existed.xml

      
    
       

    2.layout_toinfliate.xml

    
    
      
    
    

    3.LayoutInflateDemo.java

      public class LayoutInflateDemo extends Activity {
    
        private LinearLayout layoutToAdd;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_existed);
            layoutToAdd = (LinearLayout) findViewById(R.id.existedlayout);
    
            Button button = (Button) findViewById(R.id.button);
            button.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    LayoutInflater inflater = LayoutInflater
                            .from(getApplicationContext());
                    View view = inflater.inflate(R.layout.layout_toinfliate, null);
                    layoutToAdd.addView(view);
    
                }
            });
        }
    }
    

提交回复
热议问题