Android动态添加布局

时间:2014-07-20 22:30:09   收藏:0   阅读:506
//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动态添加布局,布布扣,bubuko.com

原文:http://www.cnblogs.com/niray/p/3857262.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!