android LayoutInflater.inflate()的参数介绍
来源:互联网 发布:jsp sql 动态网页 编辑:程序博客网 时间:2024/05/18 11:27
LayoutInflater大家都用很久了,但是有时候有些小问题比如inflate出来的view属性没有生效等等都困扰着大家;又比如在自定义的adapter里面inflate的布局文件高度没办法设定等等,参考了一些文章和文档,这里做一些笔记,希望大家都能补充。
LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象;
与findViewById区别:
LayoutInflater.inflate是加载一个布局文件;
findViewById则是从布局文件中查找一个控件;
一.获取LayoutInflater对象有三种方法
LayoutInflater inflater=LayoutInflater.from(context);
LayoutInflater inflater=getLayoutInflater();在Activity中可以使用,实际上是View子类下window的一个函数
LayoutInflater inflater=(LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
二:inflate 参数
public View inflate(int resource, ViewGroup root, boolean attachToRoot) :
reSource:View的layout的ID
root:需要附加到resource资源文件的根控件,inflate()会返回一个View对象,如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上。如果root为null则会忽略view根对象的LayoutParams属性(注意)。
attachToRoot:是否将root附加到布局文件的根视图上
例子:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.fragment, root,false);
或者:
v = inflater.inflate(R.layout.fragment, null);
常见问题:
有时候我们在Adapter加载自定义的view布局文件,布局文件中设置了android:layout_height="100dip",但是运行程序后发现一点作用都没有,相似的还有layout_width等以android:layout_开头的属性设置都没有作用;
因为Adapter里有一个方法是getView,这个返回的VIew是一个从XML布局里加载的,一般如下:
View v = inflater.inflate(R.layout.fragment, null);
if (root != null) { if (DEBUG) { System.out.println("Creating params from root: " + root); } // Create layout params that match root, if supplied params = root.generateLayoutParams(attrs); if (!attachToRoot) { // Set the layout params for temp if we are not // attaching. (If we are, we use addView, below) temp.setLayoutParams(params); } }
代码中可以发现:当root为null的时候是不会执行params = root.generateLayoutParams(attrs);这段代码的,这段代码就是把xml里的布局配置转为LayoutParams,换句说就是加载我们配置的布局属性,以供布局类(FrameLayout等)在onLayout的时候控制View的大小、位置、对齐等等。。以FrameLayout为例,看下它的generateLayoutParams(attrs)方法。
- android LayoutInflater.inflate()的参数介绍
- android LayoutInflater.inflate()的参数介绍
- android LayoutInflater.inflate()的参数介绍
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其功能
- ubuntu 14.04 磨合记
- 安卓ExpandableListAdapter的二级列表监听问题--自动收缩问题
- 如何优化LOG FILE SYNC
- maven学习笔记整理
- 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出
- android LayoutInflater.inflate()的参数介绍
- java的反射机制
- 2014新生暑假个人排位赛03 E. 学姐的数列
- HDU 2503 a/b + c/d
- Cocos2d-x 3.1.1 学习日志12--一Cocos2dx3.1.1移植到Android平台的方法(最有用最有效的!!)
- 【Java】多线程同步Synchronized使用分析
- 【Visual C++】游戏开发笔记之八——基础动画显示(二)游戏循环的使用
- POJ2478 Farey Sequence
- 笔记 NEURAL NETS FOR VISION CVPR 2012 Tutorial on Deep Learning