\因为在一个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");
如果组件R.id.dialog_tv是对话框上的组件,而你直接用this.findViewById(R.id.dialog_tv)肯定会报错.
三种方式可以生成LayoutInflater:
LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
然后调用inflate方法将xml布局文件转成View
public View inflate(intresource,ViewGrouproot,booleanattachToRoot)
在View类中,也有inflate方法
public static View inflate(Contextcontext,intresource,ViewGrouproot)
分享到:
相关推荐
Js算法 zip_deflate(压缩)与zip_inflate(解压),
这个demo主要实现了inflate的使用,希望可以帮助需要的同学.
matlab开发-Inflate。使用bls.gov的CPI通货膨胀计算器调整通货膨胀。
资源来自pypi官网。 资源全名:inflate-0.1rc1-py3-none-any.whl
python库,解压后可用。 资源全名:stream_inflate-0.0.13-py3-none-any.whl
LayoutInflater.from(context).inflate()方法的调研
博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/45155965
帮助你更好的理解LayoutInflate中的inflate方法,以后想怎么用怎么用
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
本文实例讲述了Android开发中setContentView和inflate的区别。分享给大家供大家参考,具体如下: 一般用LayoutInflater做一件事:inflate inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化...
Inflate与listView联合使用,自定义出一个listView,这是一个较为复杂的listView的实现,非常实用。
LayoutInflater源码分析 inflate方法的区别
Android LayoutInflater.inflate()详解 深入理解LayoutInflater.inflate() 由于我们很容易习惯公式化的预置代码,有时我们会忽略很优雅的细节。LayoutInflater以及它在Fragment的onCreateView()中填充View的方式...
资源来自pypi官网。 资源全名:stream_inflate-0.0.4-py3-none-any.whl
资源来自pypi官网。 资源全名:stream_inflate-0.0.11-py3-none-any.whl