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));

Android动态添加布局,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。