首页 > 代码库 > Android动态添加布局

Android动态添加布局

//1.利用LayoutInflater的inflate动态加载XML  mLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout_ID);  LayoutInflater layoutInflater = LayoutInflater.from(context);  View view = layoutInflater.inflate(resource--需要加载的XML, null);  XML:resource = R.layout.XML-Name;  mLinearLayout.removeAllViewsInLayout();移除当前LinearLayout的内容  mLinearLayout.addView(view);添加XML到LinearLayout设置Width和Hight:    LinearLayout.LayoutParams.FILL_PARENT、mLinearLayout.getLayoutParams().width等另:mLinearLayout.addView(v,    new LinearLayout.LayoutParams(mLinearLayout.getLayoutParams().width,     mLinearLayout.getLayoutParams().height));//2.利用View.inflate加载xml setContentView(R.layout.layout_1);      LinearLayout ll = (LinearLayout) findViewById(R.id.box_1);      View vv = View.inflate(this, R.layout.layout_2, null);      ll.addView(vv,       new LinearLayout.LayoutParams   ll.getLayoutParams ().width,ll.getLayoutParams().height));