LayoutInflate部分源码解析
LatoutInflate 主要的作用是實例化一個 xml 文件,使得開發(fā)者獲取一個 View 的實例(也可以看著把一個 xml 文件渲染成一個視圖)。在 LayoutInflate 的內(nèi)部,LayoutInflate 拿到自身的實例,一般通過 android.app.Activity#getLayoutInflater() 方法,或者通過 Context#getSystemService 來拿到已經(jīng)在應(yīng)用中的 LayoutInflate 實例(系統(tǒng)早已經(jīng)配置好了 LayoutInflate 實例)。
[源碼 1] public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {final Resources res = getContext().getResources();if (DEBUG) {Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("+ Integer.toHexString(resource) + ")");}final XmlResourceParser parser = res.getLayout(resource); //返回布局資源解析try {return inflate(parser, root, attachToRoot); // 1} finally {parser.close();}} 復制代碼在 [源碼 1] 中,inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) 方法里面三個參數(shù)分別表示:
- resource 參數(shù)表示需要渲染的 xml 布局資源
- root 如果 attachToRoot 設(shè)置為 true,則生成結(jié)構(gòu)層次的父級; 如果設(shè)置為 false,則返回結(jié)構(gòu)層次提供的一組 LayoutParams 值的對象(這個值可能為空); [ViewGroup 是一個包含其他視圖的特殊的 View,可以看作一個視圖容器]
- attachToRoot 渲染的視圖的結(jié)構(gòu)層次是否捆綁父視圖的參數(shù),如果設(shè)置為 false,則表示需要捆綁;
在 [源碼 1] 中的解析 1處,會調(diào)用方法 inflate(parser, root, attachToRoot),下面是查看其源碼:
[源碼 2] public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {synchronized (mConstructorArgs) {...final AttributeSet attrs = Xml.asAttributeSet(parser); //返回 xml 資源布局的屬性集合...View result = root;try {// 查找視圖的跟視圖節(jié)點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) { //根視圖為空 || attachToRoot 為 falsethrow new InflateException("<merge /> can be used only with a valid "+ "ViewGroup root and attachToRoot=true");}rInflate(parser, root, inflaterContext, attrs, false);} else {// 把當前 Xml 布局實例化,并且拿到該 View 的實例final View temp = createViewFromTag(root, name, inflaterContext, attrs);ViewGroup.LayoutParams params = null;if (root != null) { //當根節(jié)點不為空的時候,創(chuàng)建根視圖的節(jié)點的參數(shù)// Create layout params that match root, if suppliedparams = root.generateLayoutParams(attrs); // 2if (!attachToRoot) { //為 false 的時候根視圖綁定參數(shù)// Set the layout params for temp if we are not// attaching. (If we are, we use addView, below)temp.setLayoutParams(params); // 3}}...//再次渲染根視圖下面的子視圖rInflateChildren(parser, temp, attrs, true); ...// 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) {} catch (Exception e) {} finally {}return result;}} 復制代碼上面 [源碼 2] 主要的邏輯是把從 inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {synchronized (mConstructorArgs) 的方法中傳進的 root 參數(shù)(該參數(shù)為需要渲染的 xml 布局),經(jīng)過 createViewFromTag(root, name, inflaterContext, attrs) 的方法拿到該 root 視圖的實例,因為 root 不為空,所以在注釋 2、3 處,為該視圖布局綁定布局參數(shù),帶視圖參數(shù)返回視圖的實例; 如果 root 參數(shù)在 inflate 方法傳進來的時候為空,側(cè)直接返回該 xml 視圖的實例,但沒有綁定布局參數(shù);
轉(zhuǎn)載于:https://juejin.im/post/5ac306e9518825557e7893f6
總結(jié)
以上是生活随笔為你收集整理的LayoutInflate部分源码解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Keras进行深度学习:(二)CNN
- 下一篇: Kaggle机器学习入门实战 -- Ti