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
Layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
     android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<TextView android:id="@+id/box_0"
   android:layout_width="fill_parent" android:layout_height="40px"
   android:background="#ff00ff00" android:text="This is main layout" />
<LinearLayout
   android:id="@+id/box_1" android:layout_width="fill_parent"
   android:layout_height="wrap_content" android:layout_below="@+id/box_0">
</LinearLayout>
</RelativeLayout>

Layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/box" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent" >

<TextView android:id="@+id/box_3" android:layout_width="fill_parent"
   android:layout_height="40px" android:background="#ffff0000"
   android:text="this is 2" />
</LinearLayout>

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

这个主要是在某一个控件里面动态加载。

3.也可以利用inflate加载menu 首先在res/menu下创建自己的Menu XML—my_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/home"
    android:title="Home"
    android:icon="@drawable/home" >
</item>
<item android:id="@+id/help" android:title="Help"
    android:icon="@drawable/help" >
</item>
<item android:id="@+id/save" android:title="Save n Quit"
    android:icon="@drawable/save_quit" >
</item>
<item android:id="@+id/exit" android:title="Quit"
    android:icon="@drawable/exit" >
</item>
</menu>

public class Welcome extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
}

@Override
   public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
     case R.id.home:
     // TODO
     break;
      case R.id.help:
      // TODO
     break;
     case R.id.save:
     // TODO
    break;
     case R.id.exit:
     // TODO
    break;
   }
   return true;
   }
}

4.两种方法:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_sms,
   (ViewGroup) findViewById(R.id.toast_sms_root));

LayoutInflater mInflater = LayoutInflater.from(context);
View myView = mInflater.inflate(R.layout.customToast, null);
TextView sender = (TextView) myView.findViewById(R.id.sender);
TextView message = (TextView) myView.findViewById(R.id.message);

动态添加组件(XML)的更多相关文章

  1. Angular使用总结 --- 通过指令动态添加组件

    之前自己写的公共组件,都是会先引入,需要调起的时候再通过service控制公共组件状态.值.回调函数什么的.但是有一些场景不适合这种方式,还是动态添加组件更加好.通过写过的一个小组件来总结下. 创建组 ...

  2. easyui 动态添加组件 要重新渲染

    做项目时动态添加组件是常有的事,easyui动态添加组件时样式会失效,这是因为这个组件没有经过 easyui的解析器解析, 比如:   <pre name="code" cl ...

  3. 使用js动态添加组件

    在文章开始之前,我想说两点 1 自己初学js,文章的内容在大神看来可能就是不值一提,但是谁都是从hello world来的,望高   手不吝指教# 2 我知道这个标题起的比较蛋疼,大家看图就能说明问题 ...

  4. Android笔记(六十一)动态添加组件

    想要一个功能,点击按钮,可以在已有的布局上,新添加一组组件. 动态的创建组件,本质上还是创建组件,只不过是在程序中根据逻辑来创建.大致步骤是找到要创建控件的位置,然后将要创建的组件添加进去. 看代码: ...

  5. vue2.0动态添加组件

    方法一.<template> <input type="text" v-model='componentName'> <button @click=' ...

  6. easyui 使用jquery动态添加组件样式问题

    可以使用$.parser.parse();这个方法进行处理: 例如:   $.parser.parse(); 表示对整个页面重新渲染,渲染完就可以看到easyui原来的样式了:   var targe ...

  7. Eclipse 动态添加web.xml

    在创建javaweb项目时,也许会忘记让项目创建web.xml文件,这是我们可以右键创建后的项目,在javaEE Tools中选择第二个即可. Generate deployment Descript ...

  8. [deviceone开发]-动态添加组件add方法的示例

    一.简介 这个示例详细介绍ALayout的add方法的使用(原理也适用于Linearlayout),以及add上去的新ui和已有的ui如何数据交换,初学者推荐.二.效果图 三.相关下载 https:/ ...

  9. svg web拓扑更新了,支持动态添加svg组件

    版本1.0请点此 预览地址 https://svg.yaolunmao.top 如何使用 # 克隆项目 git clone https://github.com/yaolunmao/vue-webto ...

随机推荐

  1. 基于.NET平台的分布式应用程序的研究

    摘 要:.NET框架是Microsoft用于生成分布式Web应用程序和Web服务的下一代平台.概述了用于生成分布式应用程序的.NET框架的基本原理.重点讲述了.NET框架的基础:公共语言运行时(CLR ...

  2. [HDU 1430] 魔板

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  3. App性能优化

    http://www.cocoachina.com/ios/20150429/11712.html http://blog.csdn.net/jasonblog/article/details/765 ...

  4. 对SharePoint 2007数据库中一些数据表的使用

    转:http://blog.csdn.net/ma_jiang/article/details/6553392 在工作中接触了一些SharePoint的数据库中的一些表.在此做个总结. 一位高手告诉我 ...

  5. STL总结之bitset

    STL的bitset是一个对位进行存储和操作的容器,可以轻松对bit位进行访问.   bitset的模板声明如下: template<size_t _Bits> class bitset; ...

  6. iOS开发学习记录【整理】

    ◆ 开发环境基于 MacBook / Mac OS 10.10 / Xcode 6.1 / iOS 8 1.关于@property 在 .h 里声明了@property之后,默认 .m 不需要写@sy ...

  7. xilinx cpld XC95144XL 最小系统板

    手上有几块xilinx的CPLD芯片XC95144,闲着无聊,打样的几块板子回来玩玩. 全部引脚引出,外接4个LED灯和一个Power灯,做成了50mm*50mm的板子,省钱(O(∩_∩)O). 下面 ...

  8. [CODEVS3299]有序数组合并求第K大问题

    题目描述 Description 给出两个有序数组A和B(从小到大有序),合并两个有序数组后新数组c也有序,询问c数组中第k大的数 假设不计入输入输出复杂度,你能否给出一个O(logN)的方法? 输入 ...

  9. 关于VNC黑屏的问题

    注意: 1.vncserver启动后生成的ID号(1,2,3)要和VNCview里面填入的 ip:ID要保持一致  不然看到的就是黑屏 比如:vncserver启动后 产生: localhost.lo ...

  10. linux统计文件夹某一些文件的大小总和

    du -m smallgame_2006* | awk '{sum += $1}; END{print sum}' -m代表单位是MB, awk命令需要'',且命令需换行