android-LayoutInflater解析&inflate(resId,root,isAttachRoot)三个参数的解析。

首先大家看鸿洋的微博:

http://blog.csdn.net/lmj623565791/article/details/38171465

点击打开链接


看了这篇博客我的理解:

看源码,一切都在源码里说:


方法最终返回的是View:result



    public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {  
           synchronized (mConstructorArgs) {  
               final AttributeSet attrs = Xml.asAttributeSet(parser);  
               Context lastContext = (Context)mConstructorArgs[0];  
               mConstructorArgs[0] = mContext;  
               View result = root;  //最终返回的view。
      
               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);  //看这里,把我们的布局当做一个子view填充到Root里,
                                                           //最终返回的是Root。在Root里填充Root,
                                                                         //在listView里当然不行。
                       }  
      
                       // Decide whether to return the root that was passed in or the  
                       // top view found in xml.  
                       if (root == null || !attachToRoot) {  
                           result = temp;  //看这里,用我们自己的布局替换了Root。最终返回的是我们的view。
                       }  
                   }  
      
               } 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;  
               }  
      
               return result;  
           }  
       }  



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值