LayoutInflater.inflate方法解析
2015-02-03 19:10
507 查看
LayoutInflater.inflate方法应该是Android程序员最常使用的方法之一了,但是如果使用不当,你会碰见很多的坑。。。今天我就碰到了一个,我找到了解决方法,也打算把它记下来。。。
事情是这样的,我有一个
LineaLayout,然后在代码中会
inflate若干个
View添加到这个
LineaLayout中,但是坑出现了。。。
0x00 一个栗子
需要添加到LinearLayout中的View布局文件类似如下():[code]<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="80dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:background="@android:color/holo_green_dark" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_media_menu_icon" android:layout_height="24dp" android:layout_width="24dp" android:src="@drawable/ic_mv" android:layout_centerHorizontal="true"/> <TextView android:id="@+id/tv_media_menu_text" android:text="bxbxbai" style="@style/Menu_TextView"/> </RelativeLayout>
这个
View的宽固定为
80dp,高和父容器一样,然后就是
inflate这个View并且添加到这个
LinearLayout中(我故意将这个布局文件的background设置一个颜色,这样可以很清晰的看出这个View占的位置)
[code]LinearLayout layout = (LinearLayout)findViewById(R.id.container); View view = View.inflate(this, R.layout.layout_menu_item, null); layout.addView(view);
这样写的话,你就会发现布局文件
R.layout.layout_menu_item中的
android:layout_width="80dp"不起作用!!也就是说
View.inflate方法忽略了布局文件的宽度设置
可是你又可以发现
View.inflate方法中还有第三个参数
ViewGroup root,Android文档中是这么写的:
A view group will be the parent. Used to properly inflate the layout_* parameters
那么可以猜想肯定和这个参数有关,下面改写代码:
[code]LinearLayout layout = (LinearLayout)findViewById(R.id.container); View view = View.inflate(this, R.layout.layout_menu_item, layout); layout.addView(view);
你就会发现这样写会崩溃!然后下面这样写就没问题了:
[code]LinearLayout layout = (LinearLayout)findViewById(R.id.container); View view = View.inflate(this, R.layout.layout_menu_item, layout);
View.inflate方法自动将生成的View添加到了这个
ViewGroup root中去了!!
你可以
inflate多个View,就可以看到下面这样样子了:
0x01 inflate详解
其实View.inflate方法是调用了
LayoutInflater.from(context).inflate(resource, root, root != null)方法,而inflate方法的三个参数如下:
resource: 布局文件的id,比如
R.layout.layout_menu_item
root:这是一个可选参数,resource布局文件中
layout_*参数设置的参照物就是这个root,也就是说
inflate方法会根据这个root的大小,将resource布局文件中
layout_*参数转换成一个
LayoutParam对象
attachToRoot:是否将这个生成的View添加到这个root中去
inflate方法会返回resource布局文件产生的View
上面栗子中调用了
View.inflate(Context context, int resource, ViewGroup root),这个方法本质上是调用了了
LayoutInflater.from(context).inflate(resource, root, root != null),在这个
inflate方法中可以找到下面代码:
[code]// We are supposed to attach all the views we found (int temp) // to root. Do that now. if (root != null && attachToRoot) { root.addView(temp, params); }
可见
inflate方法自动将这个生成的
View添加到了这个
root中去了
0x02 实验
因为可以调用inflate方法的途径有很多,下面就来做实验总结一下:
实验0
布局文件R.layout.layout_menu_item中
android:layout_height="match_parent"改为
android:layout_height="10dp"
[code]LinearLayout layout = (LinearLayout)findViewById(R.id.container); View v1 = LayoutInflater.from(this).inflate(R.layout.layout_menu_item, null); layout.addView(v1); // 结果: layout_height = match_parent layout_width = match_parent
实验1
布局文件R.layout.layout_menu_item中
android:layout_height="match_parent"改为
android:layout_height="10dp"
[code]LinearLayout layout = (LinearLayout)findViewById(R.id.container); View v1 = LayoutInflater.from(this).inflate(R.layout.layout_menu_item, null); layout.addView(v1, 200, 200); // 结果: layout_height = 200 layout_width = 200
实验2
布局文件R.layout.layout_menu_item中
android:layout_height值改为
match_parent
[code]LinearLayout layout = (LinearLayout)findViewById(R.id.container); View v1 = LayoutInflater.from(this).inflate(R.layout.layout_menu_item, layout, false); layout.addView(v1); // 结果: layout_height = match_parent layout_width = 80dp // v1 = RelativeLayout 因为 attachRoot = false
实验3
布局文件R.layout.layout_menu_item中
android:layout_height值改为
match_parent
[code]LinearLayout layout = (LinearLayout)findViewById(R.id.container); View v1 = LayoutInflater.from(this).inflate(R.layout.layout_menu_item, layout, true); //layout.addView(v1); // 结果: layout_height = match_parent layout_width = 80dp // 不需要layout.addView, 因为设置attachRoot=true, 生成的View自动添加到root中去了 // v1 = root 因为 attachRoot = true
0x03 源码分析
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 | public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) { synchronized (mConstructorArgs) { Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate"); final AttributeSet attrs = Xml.asAttributeSet(parser); Context lastContext = (Context)mConstructorArgs[0]; mConstructorArgs[0] = mContext; View result = root; try { // Look for the root node. int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty } if (type != XmlPullParser.START_TAG) { throw new InflateException(parser.getPositionDescription() + ": No start tag found!"); } final String name = parser.getName(); if (DEBUG) { System.out.println("**************************"); System.out.println("Creating root view: " + name); System.out.println("**************************"); } if (TAG_MERGE.equals(name)) { if (root == null || !attachToRoot) { throw new InflateException("<merge /> can be used only with a valid " + "ViewGroup root and attachToRoot=true"); } rInflate(parser, root, attrs, false); } else { // Temp is the root view that was found in the xml View temp; if (TAG_1995.equals(name)) { temp = new BlinkLayout(mContext, attrs); } else { temp = createViewFromTag(root, name, attrs); } ViewGroup.LayoutParams params = 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); } } if (DEBUG) { System.out.println("-----> start inflating children"); } // Inflate all children under temp rInflate(parser, temp, attrs, true); if (DEBUG) { System.out.println("-----> done inflating children"); } // We are supposed to attach all the views we found (int temp) // to root. Do that now. if (root != null && attachToRoot) { root.addView(temp, params); } // Decide whether to return the root that was passed in or the // top view found in xml. if (root == null || !attachToRoot) { result = temp; } } } catch (XmlPullParserException e) { InflateException ex = new InflateException(e.getMessage()); ex.initCause(e); throw ex; } catch (IOException e) { InflateException ex = new InflateException( parser.getPositionDescription() + ": " + e.getMessage()); ex.initCause(e); throw ex; } finally { // Don't retain static reference on context. mConstructorArgs[0] = lastContext; mConstructorArgs[1] = null; } Trace.traceEnd(Trace.TRACE_TAG_VIEW); return result; }} |
总之原因就在上面代码
71-74行,布局文件
R.layout.layout_menu_item生成的View会因为
attachToRoot参数为
true,就将这个生成的
View添加到
root中去,然后
inflate方法会返回这个
root的
View
看上面代码
54-60行,如果root不为null的话,就会为这个布局文件
R.layout.layout_menu_item生成一个
LayoutParam对象,如果
attachToRoot参数为
false,那么就将这个
param对象给这个布局文件的View(看55行)。如果
attachToRoot参数为
true,那么就在上面代码第70行,将这个布局文件的
View和
param参数添加到
root中。
0x04 总结
调用LayoutInflater.inflate方法,并且将root参数设置为
null,就等于忽略了xml布局文件中的
layout_×参数
如果
root不为
null,并且
attachRoot=true,那么就会根据
root生成一个布局文
件View的
LayoutParam对象,并且将这个
View添加到
root中去,并且返回这个
root的
View
因此,最好还是使用这个代码吧:
View v1 = LayoutInflater.from(this).inflate(R.layout.layout_menu_item, layout, false);
相关文章推荐
- LayoutInflater.inflate()方法解析
- LayoutInflater.inflate方法解析
- LayoutInflater.inflate方法解析
- LayoutInflater.inflate方法解析
- LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)方法解析
- [Android]inflate方法与 findViewById 方法区别 | LayoutInflater的inflate函数用法详解
- Adapter.getView中inflater.inflate(R.layout.pending_list_item, parent, false);方法处理
- Android编程之LayoutInflater的inflate方法具体解释
- Android编程之LayoutInflater的inflate方法实例
- LayoutInflater.inflate() 使用方法
- LayoutInflater中四种类型inflate方法的介绍
- Android编程之LayoutInflater的inflate方法实例
- LayoutInflater中四种类型inflate方法的 4000 介绍
- LayoutInflater和inflate()方法的用法
- LayoutInflater的inflate方法引起的listview界面布局问题
- LayoutInflater的inflate方法实例
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题
- Android layoutinflater inflate()方法
- LayoutInflater的inflate方法
- LayoutInflater 中的 inflate() 方法