码迷,mamicode.com
首页 > 移动开发 > 详细

Android动态添加布局

时间:2014-07-21 00:13:14      阅读:422      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   width   re   

//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

Android动态添加布局

标签:android   style   blog   color   width   re   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!