LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题
2016-11-21 15:07
525 查看
一、首先看带三个参数的inflate方法:
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
1、如果root不为null,且attachToRoot为TRUE,则会在加载的布局文件的最外层再嵌套一层root布局,这时候xml根元素的布局参数当然会起作用。
2、如果root不为null,且attachToRoot为false,则不会在加载的布局文件的最外层再嵌套一层root布局,这个root只会用于为要加载的xml的根view生成布局参数( 官方原话:If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.),
这时候xml根元素的布局参数也会起作用了!!!
3、如果root为null,则attachToRoot无论为true还是false都没意义!即xml根元素的布局参数依然不会起作用!
二、再看带两个参数的inflate方法:
public View inflate(int resource, ViewGroup root)
查看源码:
1、当root不为null时,相当于上面带三个参数的inflate方法的第2种情况
2、当root为null时,相当于上面带三个参数的inflate方法的第3种情况
三、实战—————以listview来验证上面的理论
大家肯定遇到过在ListView的item布局中设置的高度没有效果的问题。
item_lv_test.xml
adapter的getView方法:
这里你该会想一想为什么很多需要显示View的方法中都有ViewGroup这个参数。
所以有些人会说在跟布局中设置是无效的,要再嵌套一层布局。这样是错误的,会造成布局层级增多,影响性能
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
1、如果root不为null,且attachToRoot为TRUE,则会在加载的布局文件的最外层再嵌套一层root布局,这时候xml根元素的布局参数当然会起作用。
2、如果root不为null,且attachToRoot为false,则不会在加载的布局文件的最外层再嵌套一层root布局,这个root只会用于为要加载的xml的根view生成布局参数( 官方原话:If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.),
这时候xml根元素的布局参数也会起作用了!!!
3、如果root为null,则attachToRoot无论为true还是false都没意义!即xml根元素的布局参数依然不会起作用!
二、再看带两个参数的inflate方法:
public View inflate(int resource, ViewGroup root)
查看源码:
public View inflate(int resource, ViewGroup root) { return inflate(resource, root, root != null); }也就是说
1、当root不为null时,相当于上面带三个参数的inflate方法的第2种情况
2、当root为null时,相当于上面带三个参数的inflate方法的第3种情况
三、实战—————以listview来验证上面的理论
大家肯定遇到过在ListView的item布局中设置的高度没有效果的问题。
item_lv_test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dip" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test" /> </LinearLayout>
adapter的getView方法:
public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflate(R.layout.item_lv_test, null); } return convertView; }如果用上面的代码会发现设置100dp是无效的。而如果换成下面的代码就可以了。
public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflate(R.layout.item_lv_test, parent, false); } return convertView; }
这里你该会想一想为什么很多需要显示View的方法中都有ViewGroup这个参数。
所以有些人会说在跟布局中设置是无效的,要再嵌套一层布局。这样是错误的,会造成布局层级增多,影响性能
相关文章推荐
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题 - u011494050的专栏 - 博客频道 - CSDN.NET
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题
- LayoutInflater的inflate()方法参数问题
- LayoutInflater.inflate()方法的ViewGroup参数问题
- LayoutInflater的inflate方法引起的listview界面布局问题
- Android编程之LayoutInflater的inflate方法详解及其中参数的作用
- LayoutInflater的inflate方法引起的listview界面布局问题
- [读书笔记]Android LayoutInflater.inflate方法参数详解原理分析
- 获取LayoutInflater对象的方法和inflate方法的一些参数问题
- 获取LayoutInflater对象的方法和inflate方法的一些参数问题
- Android LayoutInflater.inflate()方法参数详解
- Android编程之LayoutInflater的inflate方法详解
- LayoutInflater的inflate方法详解
- LayoutInflater——inflate方法不同参数的区别
- LayoutInflater的三种实现方式及inflate的参数问题
- [Android]inflate方法与 findViewById 方法区别 | LayoutInflater的inflate函数用法详解
- Android编程之LayoutInflater的inflate方法详解
- LayoutInflater的inflate方法详解
- LayoutInflater的inflate方法详解