LayoutInflater.inflate方法解析

LayoutInflater.inflate方法应该是Android程序员最常使用的方法之一了,但是如果使用不当,你会碰见很多的坑。。。今天我就碰到了一个,我找到了解决方法,也打算把它记下来。。。
事情是这样的,我有一个LineaLayout,然后在代码中会inflate若干个View添加到这个LineaLayout中,但是坑出现了。。。

0x00 一个栗子

需要添加到LinearLayout中的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

inflate

实验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
inflate

实验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

inflate

实验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 源码分析

1

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;
    }
}

实验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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值