Layout Inflation
来源:互联网 发布:如何查询淘宝会员等级 编辑:程序博客网 时间:2024/05/29 08:48
- 框架中的示例
- 获取LayoutInflater对象有三种方法
- 关于LayoutInflater类inflateint resource ViewGroup root boolean attachToRoot方法三个参数的含义
- 参考
Layout inflation在Android上下文环境下转换XML文件成View结构对象的时候需要用到。
框架中的示例
Adapter是最常用的场景,我们经常需要使用LayoutInflater来自定义ListView(通过重写getView()方法),具体的方法签名是这样的:
getView(int position, View convertView, ViewGroup parent);
Fragment也会用到inflation操作,通过onCreateView()方法创建view的时候会用到。这个方法的签名是这样的:
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
1. 不知你有没有注意到这一点,每次Framework需要你去载入一个布局文件时,都会传入一个ViewGroup参数(最后需要绑定到的根视图),如果Layoutinflater设为自动绑定到根视图的话,会抛出一个异常。
2. 所以你想想看,如果我做绑定操作的话,为什么要给你一个ViewGroup参数呢?事实证明父视图在这个inflation操作过程中是很重要的,它会计算被载入的XML在根元素中的LayoutParams,如果传入null话,就等于是告诉框架“我不知道载入的View要放到哪个父视图中”。
3.问题在于,android:layout_xxx属性会在父视图对象中被重新计算,结果就是所有你定义的LayoutParams都会被忽略掉(因为没有已知的父视图对象)。然后你就纳闷“为什么框架会忽略掉我自己定义的布局属性呢?还是去StackOverFlow上看看,提一个bug吧”。
如果没有设置LayoutParams,那么最终ViewGroup也会给你生成一个默认的属性,幸运的话(很多时候),这些默认的设置正好和你在XML文件中定义的一样……所以你就察觉不到其实已经出现问题了。
多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下:
inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。
1.获取LayoutInflater对象有三种方法
LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
2.关于LayoutInflater类inflate(int resource, ViewGroup root, boolean attachToRoot)方法三个参数的含义
resource:需要加载布局文件的id,意思是需要将这个布局文件中加载到Activity中来操作。
root:需要附加到resource资源文件的根控件,什么意思呢,就是inflate()会返回一个View对象,如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上,比如是一个LinearLayout或者其它的Layout对象。
attachToRoot:是否将root附加到布局文件的根视图上
参考
[什么时候传入null][http://blog.jobbole.com/72156/]
[各个参数的意义][http://www.2cto.com/kf/201407/313054.html]
- Layout Inflation
- Layout Inflation As Intended
- Layout Inflation as Intended
- Layout Inflation as Intended
- Layout Inflation不能这么用
- Layout Inflation不能这么用
- Layout Inflation不能这么用
- Layout inflation的正确使用
- Layout Inflation不能这么用
- Layout Inflation不能这么用
- Layout Inflation不能这么用
- 【安卓开发】Layout Inflation不能这么用
- Layout Inflation三个写法以及不同参数之间的不同之处
- Layout inflation is the term used within the context of Android to indicate when an XML layout resou
- Layout inflation is the term used within the context of Android to indicate when an XML layout resou
- usaco_score inflation
- Score Inflation
- Layout inflation在Android环境中是一个专业术语,用于表示某个XML资源布局文件被解析并转化成为一个层级的View对象的过程。
- HDU4336 Card Collector【容斥原理】
- UVA 699 The Falling Leaves (二叉树水题)
- swift - 函数指针的应用 - 避免重复算法
- 新装Win10没声音的处理方案
- LeetCode-15 3sum
- Layout Inflation
- #BestCoder Round #50 (div.2)
- map函数的用处以及几个基本的函数
- 从 Qt 的 delete 说开来
- 人在深圳的100怕!——谨以此文献给所有的深圳打拼者
- ExtJS ComboBox 按拼字首字母过滤
- hdu 1711 Number Sequence
- 将Eclipse代码导入到AndroidStudio的两种方式
- 操作系统——页面置换算法详解