日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

LayoutInflate部分源码解析

發布時間:2025/5/22 69 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LayoutInflate部分源码解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

LatoutInflate 主要的作用是實例化一個 xml 文件,使得開發者獲取一個 View 的實例(也可以看著把一個 xml 文件渲染成一個視圖)。在 LayoutInflate 的內部,LayoutInflate 拿到自身的實例,一般通過 android.app.Activity#getLayoutInflater() 方法,或者通過 Context#getSystemService 來拿到已經在應用中的 LayoutInflate 實例(系統早已經配置好了 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) 方法里面三個參數分別表示:

  • resource 參數表示需要渲染的 xml 布局資源
  • root 如果 attachToRoot 設置為 true,則生成結構層次的父級; 如果設置為 false,則返回結構層次提供的一組 LayoutParams 值的對象(這個值可能為空); [ViewGroup 是一個包含其他視圖的特殊的 View,可以看作一個視圖容器]
  • attachToRoot 渲染的視圖的結構層次是否捆綁父視圖的參數,如果設置為 false,則表示需要捆綁;

在 [源碼 1] 中的解析 1處,會調用方法 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 {// 查找視圖的跟視圖節點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) { //當根節點不為空的時候,創建根視圖的節點的參數// Create layout params that match root, if suppliedparams = root.generateLayoutParams(attrs); // 2if (!attachToRoot) { //為 false 的時候根視圖綁定參數// 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 參數(該參數為需要渲染的 xml 布局),經過 createViewFromTag(root, name, inflaterContext, attrs) 的方法拿到該 root 視圖的實例,因為 root 不為空,所以在注釋 2、3 處,為該視圖布局綁定布局參數,帶視圖參數返回視圖的實例; 如果 root 參數在 inflate 方法傳進來的時候為空,側直接返回該 xml 視圖的實例,但沒有綁定布局參數;

轉載于:https://juejin.im/post/5ac306e9518825557e7893f6

總結

以上是生活随笔為你收集整理的LayoutInflate部分源码解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。