动态添加组件(XML)
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)的更多相关文章
- Angular使用总结 --- 通过指令动态添加组件
之前自己写的公共组件,都是会先引入,需要调起的时候再通过service控制公共组件状态.值.回调函数什么的.但是有一些场景不适合这种方式,还是动态添加组件更加好.通过写过的一个小组件来总结下. 创建组 ...
- easyui 动态添加组件 要重新渲染
做项目时动态添加组件是常有的事,easyui动态添加组件时样式会失效,这是因为这个组件没有经过 easyui的解析器解析, 比如: <pre name="code" cl ...
- 使用js动态添加组件
在文章开始之前,我想说两点 1 自己初学js,文章的内容在大神看来可能就是不值一提,但是谁都是从hello world来的,望高 手不吝指教# 2 我知道这个标题起的比较蛋疼,大家看图就能说明问题 ...
- Android笔记(六十一)动态添加组件
想要一个功能,点击按钮,可以在已有的布局上,新添加一组组件. 动态的创建组件,本质上还是创建组件,只不过是在程序中根据逻辑来创建.大致步骤是找到要创建控件的位置,然后将要创建的组件添加进去. 看代码: ...
- vue2.0动态添加组件
方法一.<template> <input type="text" v-model='componentName'> <button @click=' ...
- easyui 使用jquery动态添加组件样式问题
可以使用$.parser.parse();这个方法进行处理: 例如: $.parser.parse(); 表示对整个页面重新渲染,渲染完就可以看到easyui原来的样式了: var targe ...
- Eclipse 动态添加web.xml
在创建javaweb项目时,也许会忘记让项目创建web.xml文件,这是我们可以右键创建后的项目,在javaEE Tools中选择第二个即可. Generate deployment Descript ...
- [deviceone开发]-动态添加组件add方法的示例
一.简介 这个示例详细介绍ALayout的add方法的使用(原理也适用于Linearlayout),以及add上去的新ui和已有的ui如何数据交换,初学者推荐.二.效果图 三.相关下载 https:/ ...
- svg web拓扑更新了,支持动态添加svg组件
版本1.0请点此 预览地址 https://svg.yaolunmao.top 如何使用 # 克隆项目 git clone https://github.com/yaolunmao/vue-webto ...
随机推荐
- ☀【移动】UC极速模式
UC浏览器的部分版本默认是“极速”模式,有何办法能控制UC自动改变其浏览模式? √http://www.zhihu.com/question/20582949 关于UC极速模式下访问网站错乱 √htt ...
- oracle中查询含字母的数据[正则表达式]
1,REGEXP_LIKE :与LIKE的功能相似2,REGEXP_INSTR :与INSTR的功能相似3,REGEXP_SUBSTR :与SUBSTR的功能相似4,REGEXP_REPLACE :与 ...
- javascript 关于cookie的操作
<script language=javascript> //获得coolie 的值 function cookie(name){ var cookieArray=document.coo ...
- C#索引器的作用及使用
1. 作用: 可以使得类和实例能够像数组那样使用一样,又称为带参属性 2. 区分 (1)索引器与数组的比较: 索引器的索引值不受类型限制.用来访问数组的索引值一定是整数,而索引器可以是其他类型的索引值 ...
- 【HTML】Intermediate1:Span&Div
1.HTML is all bout applying meaning to content. The span & div tags apply no meaning at all=mean ...
- Android SDK 离线安装方法
有朋友反映从连接直接下载安装包不能获取到最新版本(每次更新后的包地址需要重新去查找),而且经常无法访问. 最方便的方法是使用代理或vpn接入网络,即可及时下载最新版sdk. 作为一名开发人员,流畅地浏 ...
- (2)I2c总线SDA\SCL以及开始终止条件
I2C只用两条线(SDA和SCL)在连接到总线上的设备之间传送数据.每一个设备都由唯一的地址来识别(不管是微处理器.LCD驱动器.存储器或者键盘接口),并且可以依照设备的功能作为发送器或者接收器使用. ...
- 在今天,我们为什么还要做一个CMS
我们今天看到,在这个移动大潮席卷来的这几年,互联网以惊人的速度改变着这个世界.包括我们这个在中国互联网史上有重大影响力的“站长”,也几乎全军覆没.当然随着站长们兴起的开源CMS,到今天也都穷途末路了. ...
- WIN7中盾牌的编程-DELPHI
在PAR文件中引用UAC.RES文件(见下载地址) 代码如下: {$R uac.RES} 点击下载
- Codeforces Round #226 (Div. 2)B. Bear and Strings
/* 题意就是要找到包含“bear”的子串,计算出个数,需要注意的地方就是不要计算重复. */ 1 #include <stdio.h> #include <string.h> ...