Android中LayoutInflater.inflater()的正確打開方式
前言
LayoutInflater在開發(fā)中使用頻率很高,但是一直沒有太知道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; }
其實(shí)就是從Context中獲取Context.LAYOUT_INFLATER_SERVICE所對(duì)應(yīng)的系統(tǒng)服務(wù)。這里涉及到Context實(shí)現(xiàn)以及服務(wù)創(chuàng)建的源碼,不繼續(xù)深究。
重點(diǎn)是通常所使用的inflate()
方法,比較常用的就是這兩個(gè):
inflate(@LayoutRes int resource, @Nullable ViewGroup root)
inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
另外兩個(gè)方法inflate(XmlPullParser parser, @Nullable ViewGroup root)
和inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)
,
而兩個(gè)參數(shù)的方法,實(shí)際也是調(diào)用了三個(gè)參數(shù)的inflate()
方法,只是在三個(gè)參數(shù)傳入了root!=null
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) { return inflate(resource, root, root != null); }
那我們就可以直接看三個(gè)參數(shù)的inflate()
方法了,其中res.getLayout(resource)
這句代碼,已經(jīng)將我們傳入的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(); } }
這里其實(shí)就會(huì)發(fā)現(xiàn),最后return調(diào)用的其實(shí)是inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)
這個(gè)方法,所謂的四個(gè)inflate()
方法,其他三個(gè)只是對(duì)這個(gè)方法的重載,主要代碼還是在這個(gè)方法中實(shí)現(xiàn)的
這部分代碼較長,以注釋的形式解釋代碼
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對(duì)象轉(zhuǎn)換成AttributeSet final AttributeSet attrs = Xml.asAttributeSet(parser); Context lastContext = (Context) mConstructorArgs[0]; mConstructorArgs[0] = inflaterContext; View result = root; try { //2.在xml中尋找根節(jié)點(diǎn),如果類型是XmlPullParser.START_TAG或者XmlPullParser.END_DOCUMENT就會(huì)退出循環(huán) int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty } //3.如果根節(jié)點(diǎn)類型不是XmlPullParser.START_TAG將拋出異常 if (type != XmlPullParser.START_TAG) { throw new InflateException(parser.getPositionDescription() + ": No start tag found!"); } final String name = parser.getName(); //4.判斷根節(jié)點(diǎn)是否是merge標(biāo)簽 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.通過根節(jié)點(diǎn)創(chuàng)建臨時(shí)的view對(duì)象 final View temp = createViewFromTag(root, name, inflaterContext, attrs); ViewGroup.LayoutParams params = null; if (root != null) { //6.如果root不為空,則調(diào)用generateLayoutParams(attrs)獲取root所對(duì)應(yīng)LayoutParams對(duì)象 params = root.generateLayoutParams(attrs); //是否attachToRoot if (!attachToRoot) { //7.如果attachToRoot為false,則使用root默認(rèn)的LayoutParams作為臨時(shí)view對(duì)象的屬性 temp.setLayoutParams(params); } } //8.inflate xml的所有子節(jié)點(diǎn) rInflateChildren(parser, temp, attrs, true); //9.判斷是否需要將創(chuàng)建的臨時(shí)view attach到root中 if (root != null && attachToRoot) { root.addView(temp, params); } //10.決定方法的返回值是root還是臨時(shí)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中跟布局的屬性數(shù)據(jù)。
6,7則是很多時(shí)候使用inflate方法之后,發(fā)現(xiàn)xml布局設(shè)置的寬高屬性不生效的部分原因,有時(shí)候在RecyclerView中添加就會(huì)這樣。如果root!=null
且attachToRoot為false時(shí),創(chuàng)建的view則會(huì)具有自身根節(jié)點(diǎn)屬性值,與root對(duì)應(yīng)的LayoutParam
9的判斷決定了創(chuàng)建的view是否添加到root中,而10則決定了方法返回的是root還是view
總結(jié)
根據(jù)inflate的參數(shù)不同可以獲得不同的返回值
root | attachToRoot | 返回值 |
---|---|---|
null | false(或者true) | 返回resource對(duì)應(yīng)的view對(duì)象,但是xml中根節(jié)點(diǎn)的屬性沒有生效 |
!=null | false | 返回resource對(duì)應(yīng)的view對(duì)象,并且xml中根節(jié)點(diǎn)的屬性生效,view對(duì)象的LayoutParam與root的LayoutParam對(duì)應(yīng) |
!=null | true | 返回root對(duì)象,對(duì)應(yīng)resource創(chuàng)建的view對(duì)象,xml中根節(jié)點(diǎn)的屬性生效,并且將會(huì)添加到root中 |
注意:attachToRoot默認(rèn)為root!=null
的值
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Android開發(fā)實(shí)現(xiàn)自定義Toast、LayoutInflater使用其他布局示例
- Android 中LayoutInflater.inflate()方法的介紹
- Android中使用LayoutInflater要注意的一些坑
- Android布局加載之LayoutInflater示例詳解
- Android LayoutInflater加載布局詳解及實(shí)例代碼
- Android LayoutInflater深入分析及應(yīng)用
- Android LayoutInflater.inflate()詳解及分析
- Android 老生常談LayoutInflater的新認(rèn)知
相關(guān)文章
Android編程實(shí)現(xiàn)定時(shí)發(fā)短信功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)定時(shí)發(fā)短信功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android定時(shí)發(fā)送短信功能的相關(guān)原理、實(shí)現(xiàn)方法與注意事項(xiàng),需要的朋友可以參考下2017-09-09Android中讀取中文字符的文件與文件讀取相關(guān)介紹
InputStream.available()得到字節(jié)數(shù),然后一次讀取完,用BufferedReader.readLine()行讀取再加換行符,最后用StringBuilder.append()連接成字符串,更多祥看本文2013-06-06Android自定義控件實(shí)現(xiàn)簡單滑動(dòng)開關(guān)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)簡單滑動(dòng)開關(guān)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02android時(shí)間選擇控件之TimePickerView使用方法詳解
這篇文章主要為大家詳細(xì)介紹了android時(shí)間選擇控件之TimePickerView的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android編程實(shí)現(xiàn)圖片放大縮小功能ZoomControls控件用法實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)圖片放大縮小功能ZoomControls控件用法,結(jié)合具體實(shí)例形式分析了Android ZoomControls控件實(shí)現(xiàn)圖片縮放的具體操作方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-09-09Android使用SharedPreferences存儲(chǔ)XML文件的實(shí)現(xiàn)方法
這篇文章主要介紹了Android使用SharedPreferences存儲(chǔ)XML文件的實(shí)現(xiàn)方法,實(shí)例分析了SharedPreferences類的基本初始化與文件存儲(chǔ)相關(guān)技巧,需要的朋友可以參考下2016-07-07Android中分析Jetpack?Compose動(dòng)畫內(nèi)部的實(shí)現(xiàn)原理
這篇文章主要介紹了Android中分析Jetpack?Compose動(dòng)畫內(nèi)部的實(shí)現(xiàn)原理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-09-09Android開發(fā)5:應(yīng)用程序窗口小部件App Widgets的實(shí)現(xiàn)(附demo)
本篇文章主要介紹了android應(yīng)用程序窗口小部件App Widgets的實(shí)現(xiàn),具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11Android實(shí)現(xiàn)按鈕點(diǎn)擊事件的三種方法總結(jié)
Button是程序用于和用戶進(jìn)行交互的一個(gè)重要控件。既然有Button,那肯定有onClick方法,下面就教大家三種實(shí)現(xiàn)點(diǎn)擊事件的方法,感興趣的可以了解一下2022-04-04