LayoutInflater.inflate方法解析
来源:互联网 发布:php广告位 设计 编辑:程序博客网 时间:2024/05/16 06:41
LayoutInflater.inflate
方法应该是Android程序员最常使用的方法之一了,但是如果使用不当,你会碰见很多的坑。。。今天我就碰到了一个,我找到了解决方法,也打算把它记下来。。。
事情是这样的,我有一个LineaLayout
,然后在代码中会inflate
若干个View
添加到这个LineaLayout
中,但是坑出现了。。。
0x00 一个栗子
需要添加到LinearLayout中的View布局文件类似如下():
<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占的位置)
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
那么可以猜想肯定和这个参数有关,下面改写代码:
LinearLayout layout = (LinearLayout)findViewById(R.id.container);View view = View.inflate(this, R.layout.layout_menu_item, layout);layout.addView(view);
你就会发现这样写会崩溃!然后下面这样写就没问题了:
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
方法中可以找到下面代码:
// 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"
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"
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
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
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 xmlView 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 suppliedparams = 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 temprInflate(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;}}
实验3的原因在inflate详解中已经介绍过了。
总之原因就在上面代码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方法解析
- LayoutInflater.inflate()方法解析
- LayoutInflater 中的 inflate() 方法
- Android layoutinflater inflate()方法
- LayoutInflater的inflate方法
- LayoutInflater中的inflate方法
- LayoutInflater的inflate方法
- LayoutInflater的inflate方法
- LayoutInflater.inflate方法浅析
- LayoutInflater.inflate() 方法剖析
- LayoutInflater#inflate()方法详解
- LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)方法解析
- 正确使用inflate,inflate原理解析,LayoutInflater
- LayoutInflater的inflate方法实例
- Button记录
- poj2010
- 蓝桥杯 第39级台阶 dfs
- jndi调用时,各种应用服务器InitialContext的写法
- C#+Arcengine实现GP工具中的extract by mask(提取掩膜),可以实现提取shp范围的栅格数据,可用来获得shp范围的高程
- LayoutInflater.inflate方法解析
- 杭电2015 偶数求和
- java装饰设计模式的由来
- return的用法
- 当有过滤器链的时候根据在web.xml配置文件的配置顺序,一个一个得拦截目标
- 【单链表】环的入口点
- 算法路线_2
- 数据库数据 - - - - 中国省份和市
- 1-Java创建线程