Android中使用LayoutInflater要注意的一些坑
前言
在平時(shí)的開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)用LayoutInflater這個(gè)類,比如說(shuō)在Fragment$onCreateView和RecyclerView.Adapter$onCreateViewHolder中都會(huì)用到。它的用法也無(wú)非就是LayoutInflater.inflate(resourceId, root, attachToRoot),第一個(gè)參數(shù)沒(méi)什么好說(shuō)的,但第二個(gè)和第三個(gè)參數(shù)結(jié)合起來(lái)會(huì)帶來(lái)一定的迷惑性。之前有時(shí)候會(huì)發(fā)現(xiàn)界面布局上出了一些問(wèn)題,查了很久之后偶然的改動(dòng)了這兩個(gè)參數(shù),發(fā)現(xiàn)問(wèn)題解決了,然后也就過(guò)去了,并沒(méi)有去思考這是為什么,然后下次可能又重復(fù)這種困境了。
所以想在這里總結(jié)一下,避免以后繼續(xù)掉坑。
先來(lái)看看inflate方法的注釋:
/**
* Inflate a new view hierarchy from the specified xml resource. Throws
* {@link InflateException} if there is an error.
*
* @param resource ID for an XML layout resource to load (e.g.,
* <code>R.layout.main_page</code>)
* @param root Optional view to be the parent of the generated hierarchy (if
* <em>attachToRoot</em> is true), or else simply an object that
* provides a set of LayoutParams values for root of the returned
* hierarchy (if <em>attachToRoot</em> is false.)
* @param attachToRoot Whether the inflated hierarchy should be attached to
* the root parameter? If false, root is only used to create the
* correct subclass of LayoutParams for the root view in the XML.
* @return The root View of the inflated hierarchy. If root was supplied and
* attachToRoot is true, this is root; otherwise it is the root of
* the inflated XML file.
*/
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
首先需要了解的一點(diǎn)是,一個(gè)View的測(cè)量結(jié)果并不只是由它自己的layout_width和layout_height(即LayoutParams)所決定的,而是由父容器給它的約束(MeasureSpec)和它自身的LayoutParams共同決定的。
達(dá)成這個(gè)共識(shí)之后,我們?cè)賮?lái)看看它的參數(shù)。
- root:給布局文件提供一個(gè)父容器。布局文件里面總有一個(gè)元素是沒(méi)有父容器的(沒(méi)錯(cuò),就是根元素),所以需要給它提供一個(gè)父容器來(lái)幫助它完成測(cè)量工作。如果root為空的話,就會(huì)導(dǎo)致根元素中的layout_xxx全部失效,從而影響到整個(gè)布局。同時(shí),如果root為空的話,那么attachToRoot也就沒(méi)有意義了。
- attachToRoot: 如果為true,創(chuàng)建出來(lái)的布局系統(tǒng)會(huì)幫我們添加到父容器中去。為false的話,就只是給它提供約束,好讓這個(gè)布局順利完成測(cè)量等工作而已,將布局添加到父容器中去需要我們后續(xù)根據(jù)需要去手動(dòng)調(diào)用addView方法。
- 返回值:如果
root != null && attachToRoot,返回的View就是傳進(jìn)來(lái)的root,否則返回由布局文件所創(chuàng)建的View對(duì)象。
用幾個(gè)例子來(lái)說(shuō)明一下會(huì)比較好理解。Activity的布局是一個(gè)LinearLayout,要添加的布局如下:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/item_text" android:layout_width="200dp" android:layout_height="50dp" android:layout_marginLeft="16dp" android:layout_marginTop="16dp" android:background="@color/colorAccent" android:gravity="center" android:text="item: text"/>
正常的情況
// 第一種方法 View inflatedView = LayoutInflater.from(this).inflate(R.layout.item_text, mLinearLayout, true); Log.d(TAG, "inflated view is " + inflatedView); // 第二種方法 View inflatedView = LayoutInflater.from(this).inflate(R.layout.item_text, mLinearLayout, false); Log.d(TAG, "inflated view is " + inflatedView); mLinearLayout.addView(inflatedView);
視覺(jué)上的結(jié)果都是一樣的

但是Log就有一點(diǎn)不一樣了,這就是attachToRoot不同的值所導(dǎo)致的。
第一種方法的Log
D/MainActivity: inflated view is android.widget.LinearLayout{36e9aac V.E...... ......I. 0,0-0,0 #7f0c0051 app:id/linear}
第二種方法的Log
D/MainActivity: inflated view is android.support.v7.widget.AppCompatTextView{3c9d37b V.ED..... ......ID 0,0-0,0 #7f0c0054 app:id/item_text}
還有一個(gè)需要注意的地方是:如果在第一種方法的基礎(chǔ)上再加上mLinearLayout.addView(inflatedView)就會(huì)造成報(bào)錯(cuò)
IllegalStateException: The specified child already has a parent.... 。
而如果第二種方法沒(méi)有這句話,界面上是看不到任何東西的。
root為null的情況
mLinearLayout = (LinearLayout) findViewById(R.id.linear); View inflatedView = LayoutInflater.from(this).inflate(R.layout.item_text, null); Log.d(TAG, "inflated view is " + inflatedView); mLinearLayout.addView(inflatedView);

此時(shí)再看看它的布局文件:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/item_text" android:layout_width="200dp" android:layout_height="50dp" android:layout_marginLeft="16dp" android:layout_marginTop="16dp" android:background="@color/colorAccent" android:gravity="center" android:text="item: text"/>
不難發(fā)現(xiàn),所有l(wèi)ayout_xxx的屬性全都失效了。
RecyclerView中的Inflater
上面說(shuō)了,在創(chuàng)建布局的時(shí)候,要把布局添加到root中去,并且有兩種方法,但是我們?cè)趏nCreateViewHolder中添加布局的時(shí)候卻是這樣寫(xiě)的:
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text, parent, false);
return new MyViewHolder(view);
}
如果第三個(gè)參數(shù)傳了true還會(huì)報(bào)錯(cuò),這又是為什么呢?
java.lang.IllegalStateException: The specified child already has a parent.
直觀上來(lái)解釋就是,子View的添加與刪除是由RecyclerView來(lái)管理的,不需要我們來(lái)添加。但我們還是從RecyclerView的代碼來(lái)理解一下會(huì)好一些。
以LinearLayoutManager為例,RecyclerView在創(chuàng)建子View的時(shí)候會(huì)調(diào)用到LinearLayoutManager$layoutChunk方法:
void layoutChunk(RecyclerView.Recycler recycler, RecyclerView.State state,
LayoutState layoutState, LayoutChunkResult result) {
// 在這里會(huì)調(diào)用到Adapter$onCreateViewHolder
View view = layoutState.next(recycler);
if (view == null) {
if (DEBUG && layoutState.mScrapList == null) {
throw new RuntimeException("received null view when unexpected");
}
// if we are laying out views in scrap, this may return null which means there is
// no more items to layout.
result.mFinished = true;
return;
}
LayoutParams params = (LayoutParams) view.getLayoutParams();
if (layoutState.mScrapList == null) {
if (mShouldReverseLayout == (layoutState.mLayoutDirection
== LayoutState.LAYOUT_START)) {
addView(view);
} else {
addView(view, 0);
}
} else {
if (mShouldReverseLayout == (layoutState.mLayoutDirection
== LayoutState.LAYOUT_START)) {
addDisappearingView(view);
} else {
addDisappearingView(view, 0);
}
}
// 省略其它大部分代碼
}
在初始化的時(shí)候,View view = layoutState.next(recycler)里面會(huì)調(diào)用到我們熟悉的onCreateViewHolder方法,然后我們?cè)诶锩鎖nflate的過(guò)程中第三個(gè)參數(shù)傳了true,將子View添加到了RecyclerView中去了。然而,獲得View之后,調(diào)用到了addView(因?yàn)槭浅跏蓟?,不可能調(diào)用addDisappearingView) ,這里又會(huì)去添加一次,所以報(bào)出了上面的IllegalStateException異常。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開(kāi)發(fā)者們能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android TimerTask 的簡(jiǎn)單應(yīng)用及注意事項(xiàng)
這篇文章主要介紹了Android TimerTask 的簡(jiǎn)單應(yīng)用及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android基于ListView實(shí)現(xiàn)類似QQ空間的滾動(dòng)翻頁(yè)與滾動(dòng)加載效果
這篇文章主要介紹了Android基于ListView實(shí)現(xiàn)類似QQ空間的滾動(dòng)翻頁(yè)與滾動(dòng)加載效果,涉及ListView相關(guān)屬性與方法的操作技巧,需要的朋友可以參考下2016-08-08
Flutter監(jiān)聽(tīng)當(dāng)前頁(yè)面可見(jiàn)與隱藏狀態(tài)的代碼詳解
文章介紹了如何在Flutter中使用路由觀察者來(lái)監(jiān)聽(tīng)?wèi)?yīng)用進(jìn)入前臺(tái)或后臺(tái)狀態(tài)以及頁(yè)面的顯示和隱藏,并通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2025-03-03
使用Android Studio檢測(cè)內(nèi)存泄露(LeakCanary)
本篇文章主要介紹了用Android Studio檢測(cè)內(nèi)存泄露的問(wèn)題的解決方法,Android Studio在為我們提供了良好的編碼體驗(yàn)的同時(shí),也提供了許多對(duì)App性能分析的工具,下面我們一起來(lái)了解一下。2016-12-12
android自定義環(huán)形統(tǒng)計(jì)圖動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了android自定義環(huán)形統(tǒng)計(jì)圖動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Android實(shí)現(xiàn)語(yǔ)音播放與錄音功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)語(yǔ)音播放與錄音功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android編程實(shí)現(xiàn)異步消息處理機(jī)制的幾種方法總結(jié)
這篇文章主要介紹了Android編程實(shí)現(xiàn)異步消息處理機(jī)制的幾種方法,結(jié)合實(shí)例形式詳細(xì)總結(jié)分析了Android異步消息處理機(jī)制的原理、相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2018-08-08
Android 7.0 Nougat不得不知的11項(xiàng)新功能
不得不知的11項(xiàng)Android 7.0 Nougat新功能,感興趣的小伙伴們可以參考一下2016-09-09

