在实际开发中LayoutInflater这个类是非常有用的,它的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件。

而findViewById()是查找的具体 widget控件(如Button,TextView等)。

1.main.xml

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

2.dynamic_add.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="25dip"
android:layout_height="25dip"
android:background="#ff0000"
android:text="dynamic_add" />

3.activity

    private View view;

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.main);
ViewGroup parent = (ViewGroup) findViewById(R.id.vertical_container); // result: layout_height=wrap_content layout_width=match_parent
//inflate(int resource, ViewGroup root)
//inflate 返回root view,如果提供root参数则返回root,否则返回resource的root
view = LayoutInflater.from(getBaseContext())
.inflate(R.layout.dynamic_add,null);
parent.addView(view); // result: layout_height=100 layout_width=100
view = LayoutInflater.from(getBaseContext())
.inflate(R.layout.dynamic_add,null);
parent.addView(view, 100, 100); // result: layout_height=25dip layout_width=25dip
view = LayoutInflater.from(getBaseContext())
.inflate(R.layout.dynamic_add,parent, false);
parent.addView(view); // result: layout_height=25dip layout_width=25dip
// parent.addView(view) not necessary as this is already done by attachRoot=true
view = LayoutInflater.from(getBaseContext())
.inflate(R.layout.dynamic_add,parent, true);
}
   在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.如果你的Activity里如果用到别的layout,
比如对话框上的layout,你还要设置对话框上的layout里的组件(ImageView,TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,
然后再用这个layout对象去找到它上面的组件,如:
View view = View.inflate(this, R.layout.dialog_layout, null);
TextView dialogTV = (TextView) view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");

LayoutInflater的动态增加控件的更多相关文章

  1. android动态增加控件时控制样式的方法

    在学习android的动画时,发现所谓的tween动画只是改变绘制效果并不改变原控件的位置时是颇为失望的,虽然3.0之后已经有了property animation,但是由于要兼容老版本的androi ...

  2. DOM动态增加控件

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  3. 在Extjs中动态增加控件

    Ext.onReady(function () { Ext.QuickTips.init(); Ext.form.Field.prototype.msgTarget = 'side'; var aut ...

  4. Jquery 动态追加控件并获取值

    先展示通过动态添加控件的结果: 实现步骤: 1.引用js文件 <script src="Script/jquery-1.5.1.min.js" type="text ...

  5. JQuery动态添加控件并取值

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. winform导入导出excel,后台动态添加控件

    思路: 导入: 1,初始化一个OpenFileDialog类 (OpenFileDialog fileDialog = new OpenFileDialog();) 2, 获取用户选择文件的后缀名(s ...

  7. Android 在布局容器中动态添加控件

    这里,通过一个小demo,就可以掌握在布局容器中动态添加控件,以动态添加Button控件为例,添加其他控件同样道理. 1.addView 添加控件到布局容器 2.removeView 在布局容器中删掉 ...

  8. VC中动态添加控件

    VC中动态添加控件 动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的. 一.创建动态控件: 为了对照,我们先来看一下静态控件的创建. 放置静态控件时必须先建立一个 ...

  9. jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法

    博客分类: jquery-easyui jQueryAjax框架HTML  现象: AJAX返回的html无法做到自动渲染为EasyUI的样式.比如:class="easyui-layout ...

随机推荐

  1. sama5d36 OUT0-OUT3 对应关系 带光模块的系统

    ARM-IO9      PA8     OUT0 ARM-IO10    PA1     OUT1 ARM-IO11    PA3     OUT2 ARM-IO12    PA9     OUT3

  2. postgreSQL php及客户端

    yum install php-pgsql “conf/config.inc.php”) $conf['servers'][0]['host'] = 'localhost'; and $conf['e ...

  3. Run time setting设置详解

    Pacing转载自belie 1>     Run time setting设置中的Browser:‘Simulate a new user on each iteration’选项例如:录制了 ...

  4. WPF的DataGrid控件从excel里复制数据然后粘贴

    WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴.WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用 ...

  5. 用户控件(ASCX)向网页(ASPX)传值使用反射实现

    用户控件向网页传递值,方法非常之多,此博文尝试使用反射来实现.在站点中,建一个网页以及一个用户控件. 网页切换至设计模式,拉用户控件至网页上. Default.aspx: <%@ Page La ...

  6. SVN入门2

    TortoiseSVN 以简单易用的安装包的形式发布.双击安装文件并按照提示操作.安装文件会照顾其余的事情.安装结束后不要忘记重启电脑. Import(导入) 导入.导出是以服务器上的版本库为中心的. ...

  7. ios开发之--使用AFNetWorking 3.1.0 ,简单的请求封装类

    从苹果系统自带的请求类,到ASIHttpRequest第三方请求类,再到AFNetWorking第三方请求类,目前只要牵扯到数据请求,基本上都是用AFN,所以,这里纯粹是记录下: hRequestTo ...

  8. IOS实现打电话后回调

    本文转载至 http://blog.csdn.net/cerastes/article/details/38340687   UIWebView *callWebview =[[UIWebView a ...

  9. k8s滚动升级

    为了服务升级过程中提供可持续的不中断的服务,Kubernetes 提供了rolling update机制,具体配置需要修改对应服务的yaml文件 参数解析: minReadySeconds: 100 ...

  10. docker-compose安装elasticsearch集群

    文件目录: 1.编写docker-compose文件 version: '3' services: es-master: image: elasticsearch:6.4.3 container_na ...