国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中LayoutInflater.inflater()的正確打開方式

2019-10-21 21:34:06
字體:
來源:轉載
供稿:網友

前言

LayoutInflater在開發中使用頻率很高,但是一直沒有太知道LayoutInflater.from(context).inflate()的真正用法,今天就看看源碼的流程。

首先來看from()的源碼:

/** * Obtains the LayoutInflater from the given context. */public static LayoutInflater from(Context context) { LayoutInflater LayoutInflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (LayoutInflater == null) { throw new AssertionError("LayoutInflater not found."); } return LayoutInflater;}

其實就是從Context中獲取Context.LAYOUT_INFLATER_SERVICE所對應的系統服務。這里涉及到Context實現以及服務創建的源碼,不繼續深究。

重點是通常所使用的inflate()方法,比較常用的就是這兩個:

  • inflate(@LayoutRes int resource, @Nullable ViewGroup root)
  • inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

另外兩個方法inflate(XmlPullParser parser, @Nullable ViewGroup root)inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) ,

而兩個參數的方法,實際也是調用了三個參數的inflate()方法,只是在三個參數傳入了root!=null

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) { return inflate(resource, root, root != null);}

那我們就可以直接看三個參數的inflate()方法了,其中res.getLayout(resource)這句代碼,已經將我們傳入的layout布局的根布局的xml屬性都加載到了XmlResourceParser中

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) { final Resources res = getContext().getResources(); //省略代碼 final XmlResourceParser parser = res.getLayout(resource); try { return inflate(parser, root, attachToRoot); } finally { parser.close(); }}

這里其實就會發現,最后return調用的其實是inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)這個方法,所謂的四個inflate()方法,其他三個只是對這個方法的重載,主要代碼還是在這個方法中實現的

這部分代碼較長,以注釋的形式解釋代碼

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { synchronized (mConstructorArgs) { Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate"); final Context inflaterContext = mContext; //1.通過XmlResourceParser對象轉換成AttributeSet final AttributeSet attrs = Xml.asAttributeSet(parser); Context lastContext = (Context) mConstructorArgs[0]; mConstructorArgs[0] = inflaterContext; View result = root; try {  //2.在xml中尋找根節點,如果類型是XmlPullParser.START_TAG或者XmlPullParser.END_DOCUMENT就會退出循環  int type;  while ((type = parser.next()) != XmlPullParser.START_TAG &&   type != XmlPullParser.END_DOCUMENT) {  // Empty  }  //3.如果根節點類型不是XmlPullParser.START_TAG將拋出異常  if (type != XmlPullParser.START_TAG) {  throw new InflateException(parser.getPositionDescription()   + ": No start tag found!");  }  final String name = parser.getName();  //4.判斷根節點是否是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 {  //5.通過根節點創建臨時的view對象  final View temp = createViewFromTag(root, name, inflaterContext, attrs);  ViewGroup.LayoutParams params = null;  if (root != null) {   //6.如果root不為空,則調用generateLayoutParams(attrs)獲取root所對應LayoutParams對象   params = root.generateLayoutParams(attrs);   //是否attachToRoot   if (!attachToRoot) {   //7.如果attachToRoot為false,則使用root默認的LayoutParams作為臨時view對象的屬性   temp.setLayoutParams(params);   }  }  //8.inflate xml的所有子節點  rInflateChildren(parser, temp, attrs, true);  //9.判斷是否需要將創建的臨時view attach到root中  if (root != null && attachToRoot) {   root.addView(temp, params);  }  //10.決定方法的返回值是root還是臨時view  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 {  mConstructorArgs[0] = lastContext;  mConstructorArgs[1] = null;  Trace.traceEnd(Trace.TRACE_TAG_VIEW); } return result; }}

1中的XmlResourceParser在之前所獲取的,包含了layout中跟布局的屬性數據。

6,7則是很多時候使用inflate方法之后,發現xml布局設置的寬高屬性不生效的部分原因,有時候在RecyclerView中添加就會這樣。如果root!=null且attachToRoot為false時,創建的view則會具有自身根節點屬性值,與root對應的LayoutParam

9的判斷決定了創建的view是否添加到root中,而10則決定了方法返回的是root還是view

總結

根據inflate的參數不同可以獲得不同的返回值

root attachToRoot 返回值
null false(或者true) 返回resource對應的view對象,但是xml中根節點的屬性沒有生效
!=null false 返回resource對應的view對象,并且xml中根節點的屬性生效,view對象的LayoutParam與root的LayoutParam對應
!=null true 返回root對象,對應resource創建的view對象,xml中根節點的屬性生效,并且將會添加到root中

注意:attachToRoot默認為root!=null的值

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安陆市| 孟州市| 铁岭县| 资兴市| 岚皋县| 灵寿县| 京山县| 平安县| 恩施市| 璧山县| 安庆市| 永顺县| 固安县| 沾化县| 延寿县| 瑞丽市| 莒南县| 临泉县| 思南县| 嫩江县| 米泉市| 靖西县| 吉木萨尔县| 虹口区| 华蓥市| 历史| 榆社县| 化德县| 台安县| 茌平县| 云林县| 台中县| 松潘县| 南通市| 庄河市| 台前县| 兴文县| 邛崃市| 贵州省| 长沙市| 德江县|