1. 程式人生 > >layoutInflater引數解析與原始碼分析

layoutInflater引數解析與原始碼分析

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { synchronized (mConstructorArgs) { Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate"); final Context inflaterContext = mContext; final AttributeSet attrs = Xml.asAttributeSet(parser); Context lastContext
= (Context) mConstructorArgs[0]; mConstructorArgs[0] = inflaterContext; //這裡的result就是最終該方法要返回的view,初始時設為第二個引數root View result = root; try { // Look for the root node. int type; //在while中找到了parser的根節點。尋找方法既不是起始標誌,也不是結束標誌,它就是最開始的根節點。
//後面的節點都是它的子節點。這個節點對應的是xml佈局中最外層的佈局。 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("**************************"); } //你的佈局最外層是否merge標籤,不是的話,不用看裡面的 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, inflaterContext, attrs, false); } else { // Temp is the root view that was found in the xml //這裡返回的是xml佈局根節點的對應的view。其中createViewFromTag()是通過name找到類載入器,然後利用反射 //建立name對應的view。如<TextView>就會建立TextView物件。 final View temp = createViewFromTag(root, name, inflaterContext, 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 //這裡的root經常讓人理解為xml生成view的根節點,上面的temp才是那個根節點,這裡的root你是傳進來的第二個 //引數viewGroup,它更第一個引數沒半毛錢關係。這裡建立root的佈局引數。 params = root.generateLayoutParams(attrs); if (!attachToRoot) { // Set the layout params for temp if we are not // attaching. (If we are, we use addView, below) //這裡的情況對應的是第二個引數不為null,且第3個引數為false,這是getView裡的最常用的寫法。 //temp與root的區別解釋上面說了,之所以為temp設定佈局引數需要傳root的params,你因為你想要temp的layout屬性 //生效,就必須給他的外層加個容器,這裡指root,只有存在這個容器,它的layout屬性才有意義。 temp.setLayoutParams(params); } } if (DEBUG) { System.out.println("-----> start inflating children"); } // Inflate all children under temp against its context. //這個方法的任務就是將xml裡的所有子節點全部填充成view,並呼叫addView新增到temp裡面。 //實現的邏輯根據當前xml標籤名生成對應的view和viewGroup,如果是view就直接新增到temp裡,如果是viewGroup //繼續呼叫rInflateChildren,直至解析完它的子view。 rInflateChildren(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. //前面提到過 listview(也就是這裡的root).addView會報錯,當我們getView第二個引數不為null,且第3個引數為true, //就是觸發了這裡的條件 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. //官方的註釋說的很明白:決定是返回root還是我們xml的頂級view(temp)。 //如果滿足以下條件,就返回頂級view--temp。在上面的例子中,只要root為null,不管第3個引數為true還是false,這個if都會成立, //結果返回沒有外層容器的頂級view。因此它的layout屬性就失效了。 if (root == null || !attachToRoot) { result = temp; } } } catch (XmlPullParserException e) { final InflateException ie = new InflateException(e.getMessage(), e); ie.setStackTrace(EMPTY_STACK_TRACE); throw ie; } catch (Exception e) { final InflateException ie = new InflateException(parser.getPositionDescription() + ": " + e.getMessage(), e); ie.setStackTrace(EMPTY_STACK_TRACE); throw ie; } finally { // Don't retain static reference on context. mConstructorArgs[0] = lastContext; mConstructorArgs[1] = null; Trace.traceEnd(Trace.TRACE_TAG_VIEW); } return result; } }