天天看點

android 在布局中動态添加控件

第一步

Java代碼 

final LayoutInflater inflater = LayoutInflater.from(this);  

第二步:擷取需要被添加控件的布局

final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01);  

第三步:擷取需要添加的布局(控件)

LinearLayout layout = (LinearLayout) inflater.inflate(  

                        R.layout.listview, null).findViewById(R.id.layout);  

第四步:添加到布局中

lin.removeAllViews();  

                lin.addView(layout);