Inflate加载布局文件

From:http://blog.csdn.net/jojoyce/article/details/16844919

一、View.inflate和Layoutinflater.inflate有什么联系么?

它们之间的联系看下代码就一目了然了:
  1. /** 
  2.  * Inflate a view from an XML resource. This convenience method wraps the {@link 
  3.  * LayoutInflater} class, which provides a full range of options for view inflation. 
  4.  * 
  5.  * @param context The Context object for your activity or application. 
  6.  * @param resource The resource ID to inflate 
  7.  * @param root A view group that will be the parent. Used to properly inflate the 
  8.  * layout_* parameters. 
  9.  * @see LayoutInflater 
  10.  */  
  11. public static View inflate(Context context, int resource, ViewGroup root) {  
  12.     LayoutInflater factory = LayoutInflater.from(context);  
  13.     return factory.inflate(resource, root);  
  14. }  
    /**
     * Inflate a view from an XML resource. This convenience method wraps the {@link
     * LayoutInflater} class, which provides a full range of options for view inflation.
     *
     * @param context The Context object for your activity or application.
     * @param resource The resource ID to inflate
     * @param root A view group that will be the parent. Used to properly inflate the
     * layout_* parameters.
     * @see LayoutInflater
     */
    public static View inflate(Context context, int resource, ViewGroup root) {
        LayoutInflater factory = LayoutInflater.from(context);
        return factory.inflate(resource, root);
    }

二、LayoutInflater中的四个inflate方法的有什么不同?

inflate加载xml文件并不是当你调用inflate方法时去解析一个xml纯文件,而是有一个预编译的过程。
  1. public View inflate(int resource, ViewGroup root) {  
  2.     return inflate(resource, root, root != null);  
  3. }  
  4. public View inflate(int resource, ViewGroup root, boolean attachToRoot) {  
  5.     if (DEBUG) System.out.println("INFLATING from resource: " + resource);  
  6.     XmlResourceParser parser = getContext().getResources().getLayout(resource);  
  7.     try {  
  8.         return inflate(parser, root, attachToRoot);  
  9.     } finally {  
  10.         parser.close();  
  11.     }  
  12. }  
  13. public View inflate(XmlPullParser parser, ViewGroup root) {  
  14.     return inflate(parser, root, root != null);  
  15. }  
  16.   
  17. public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {  
  18.     synchronized (mConstructorArgs) {  
  19.         Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");  
  20.   
  21.         final AttributeSet attrs = Xml.asAttributeSet(parser);  
  22.         Context lastContext = (Context)mConstructorArgs[0];  
  23.         mConstructorArgs[0] = mContext;  
  24.         View result = root;  
  25.   
  26.         try {  
  27.             // Look for the root node.   
  28.             int type;  
  29.             while ((type = parser.next()) != XmlPullParser.START_TAG &&  
  30.                     type != XmlPullParser.END_DOCUMENT) {  
  31.                 // Empty   
  32.             }  
  33.   
  34.             if (type != XmlPullParser.START_TAG) {  
  35.                 throw new InflateException(parser.getPositionDescription()  
  36.                         + ": No start tag found!");  
  37.             }  
  38.   
  39.             final String name = parser.getName();  
  40.               
  41.             if (DEBUG) {  
  42.                 System.out.println("**************************");  
  43.                 System.out.println("Creating root view: "  
  44.                         + name);  
  45.                 System.out.println("**************************");  
  46.             }  
  47.   
  48.             if (TAG_MERGE.equals(name)) {  
  49.                 if (root == null || !attachToRoot) {  
  50.                     throw new InflateException("<merge /> can be used only with a valid "  
  51.                             + "ViewGroup root and attachToRoot=true");  
  52.                 }  
  53.   
  54.                 rInflate(parser, root, attrs, false);  
  55.             } else {  
  56.                 // Temp is the root view that was found in the xml   
  57.                 View temp;  
  58.                 if (TAG_1995.equals(name)) {  
  59.                     temp = new BlinkLayout(mContext, attrs);  
  60.                 } else {  
  61.                     temp = createViewFromTag(root, name, attrs);  
  62.                 }  
  63.   
  64.                 ViewGroup.LayoutParams params = null;  
  65.   
  66.                 if (root != null) {  
  67.                     if (DEBUG) {  
  68.                         System.out.println("Creating params from root: " +  
  69.                                 root);  
  70.                     }  
  71.                     // Create layout params that match root, if supplied   
  72.                     params = root.generateLayoutParams(attrs);  
  73.                     if (!attachToRoot) {  
  74.                         // Set the layout params for temp if we are not   
  75.                         // attaching. (If we are, we use addView, below)   
  76.                         temp.setLayoutParams(params);  
  77.                     }  
  78.                 }  
  79.   
  80.                 if (DEBUG) {  
  81.                     System.out.println("-----> start inflating children");  
  82.                 }  
  83.                 // Inflate all children under temp   
  84.                 rInflate(parser, temp, attrs, true);  
  85.                 if (DEBUG) {  
  86.                     System.out.println("-----> done inflating children");  
  87.                 }  
  88.   
  89.                 // We are supposed to attach all the views we found (int temp)   
  90.                 // to root. Do that now.   
  91.                 if (root != null && attachToRoot) {  
  92.                     root.addView(temp, params);  
  93.                 }  
  94.   
  95.                 // Decide whether to return the root that was passed in or the   
  96.                 // top view found in xml.   
  97.                 if (root == null || !attachToRoot) {  
  98.                     result = temp;  
  99.                 }  
  100.             }  
  101.   
  102.         } catch (XmlPullParserException e) {  
  103.             InflateException ex = new InflateException(e.getMessage());  
  104.             ex.initCause(e);  
  105.             throw ex;  
  106.         } catch (IOException e) {  
  107.             InflateException ex = new InflateException(  
  108.                     parser.getPositionDescription()  
  109.                     + ": " + e.getMessage());  
  110.             ex.initCause(e);  
  111.             throw ex;  
  112.         } finally {  
  113.             // Don't retain static reference on context.   
  114.             mConstructorArgs[0] = lastContext;  
  115.             mConstructorArgs[1] = null;  
  116.         }  
  117.   
  118.         Trace.traceEnd(Trace.TRACE_TAG_VIEW);  
  119.   
  120.         return result;  
  121.     }  
  122. }  
    public View inflate(int resource, ViewGroup root) {
        return inflate(resource, root, root != null);
    }
    public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
        if (DEBUG) System.out.println("INFLATING from resource: " + resource);
        XmlResourceParser parser = getContext().getResources().getLayout(resource);
        try {
            return inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }
    public View inflate(XmlPullParser parser, ViewGroup root) {
        return inflate(parser, root, root != null);
    }

    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;
        }
    }
这篇博客是深入分析一个View的创建过程,博主分析得很透彻: http://zhouyunan2010.iteye.com/blog/1488725

三、inflate()中各个参数代表什么? 

  1.     public View inflate(int resource, ViewGroup root) {  
  2.         return inflate(resource, root, root != null);  
  3.     }  
  4.    public View inflate(int resource, ViewGroup root, boolean attachToRoot)  
  5. resource参数很好理解,可能不好理解就是root和attachToRoot两个参数,通过分析源码我们可以知道:  
  6. View result = root;  
  7. View temp;  
  8.  if (TAG_1995.equals(name)) {  
  9.         temp = new BlinkLayout(mContext, attrs);  
  10.  } else {  
  11.         temp = createViewFromTag(root, name, attrs);  
  12.  }  
  13. if (root == null || !attachToRoot) {  
  14.         result = temp;  
  15. }  
  16. if (root != null && attachToRoot) {  
  17.        root.addView(temp, params);  
  18. }                     
  19. return result;  
    public View inflate(int resource, ViewGroup root) {
        return inflate(resource, root, root != null);
    }
   public View inflate(int resource, ViewGroup root, boolean attachToRoot)
resource参数很好理解,可能不好理解就是root和attachToRoot两个参数,通过分析源码我们可以知道:
View result = root;
View temp;
 if (TAG_1995.equals(name)) {
        temp = new BlinkLayout(mContext, attrs);
 } else {
        temp = createViewFromTag(root, name, attrs);
 }
if (root == null || !attachToRoot) {
        result = temp;
}
if (root != null && attachToRoot) {
       root.addView(temp, params);
}                   
return result;

也就是说:假如root设置为null或者root!=nul但attachToRoot设置为false,则这个xml加载出来的view不会挂载到别的视图节点上去;
当root!=null并且attachToRoot设置为true时,就会挂载上去,return为root
root!=nul但attachToRoot设置为false时,这个root用来view生成正确的params布局参数来match root。
  1. if (root != null) {  
  2.     if (DEBUG) {  
  3.         System.out.println("Creating params from root: " +  
  4.                 root);  
  5.     }  
  6.     // Create layout params that match root, if supplied   
  7.     params = root.generateLayoutParams(attrs);  
  8.     if (!attachToRoot) {  
  9.         // Set the layout params for temp if we are not   
  10.         // attaching. (If we are, we use addView, below)   
  11.         temp.setLayoutParams(params);  
  12.     }  
  13. }  

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值