通俗的说,inflate就相当于将一个xml中定义的布局找出来.
因为在一个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 (int resource, ViewGroup root, boolean attachToRoot)
在View类中,也有inflate方法
public static View inflate (Context context, int resource, ViewGroup root)
分享到:
相关推荐
这个demo主要实现了inflate的使用,希望可以帮助需要的同学.
Js算法 zip_deflate(压缩)与zip_inflate(解压),
matlab开发-Inflate。使用bls.gov的CPI通货膨胀计算器调整通货膨胀。
资源来自pypi官网。 资源全名:inflate-0.1rc1-py3-none-any.whl
帮助你更好的理解LayoutInflate中的inflate方法,以后想怎么用怎么用
python库,解压后可用。 资源全名:stream_inflate-0.0.13-py3-none-any.whl
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
LayoutInflater.from(context).inflate()方法的调研
博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/45155965
Inflate与listView联合使用,自定义出一个listView,这是一个较为复杂的listView的实现,非常实用。
LayoutInflater源码分析 inflate方法的区别
本文实例讲述了Android开发中setContentView和inflate的区别。分享给大家供大家参考,具体如下: 一般用LayoutInflater做一件事:inflate inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化...
Android LayoutInflater.inflate()详解 深入理解LayoutInflater.inflate() 由于我们很容易习惯公式化的预置代码,有时我们会忽略很优雅的细节。LayoutInflater以及它在Fragment的onCreateView()中填充View的方式...
本文主要介绍Android 中Inflate 方法的用法, 在开发Android应用过程中,可以在程序中应用 Inflate()方法加载新布局,希望能帮助有需要的朋友
Android 中LayoutInflater.inflate()方法的介绍 最近一直想弄明白LayoutInflater对象的inflate方法的用法,今天做了实例。 android:layout_width=match_parent android:layout_height=200dp android:...
Android getViewById和getLayoutInflater().inflate()的详解及比较 由于本人刚刚学习Android 对于getViewById和getLayoutInflater().inflate()的方法该如何使用不知如何分别,这里就上网查下资料整理下,大家可以...
主要介绍了Android LayoutInflater.inflate源码分析的相关资料,需要的朋友可以参考下
资源来自pypi官网。 资源全名:stream_inflate-0.0.4-py3-none-any.whl