一、LayoutInflater.inflate()方法兩個參數(shù)和三個參數(shù)的區(qū)別
LayoutInflater.inflate() 方法是用于將 XML 布局文件轉(zhuǎn)換為 Android 中的 View 對象。其具有兩個重載版本:一個是接受兩個參數(shù)的版本,另一個是接受三個參數(shù)的版本。
inflate(int resource, ViewGroup root) 方法接受兩個參數(shù): resource 和 root 。其中 resource 指定需要轉(zhuǎn)換的布局文件的資源 ID, root 是該文件在當(dāng)前布局中的根視圖,即父級視圖。如果 root 參數(shù)為 null ,則將忽略此參數(shù)并將 resource 中定義的布局文件最外層的根視圖設(shè)置為新 View 對象的父級視圖。inflate(int resource, ViewGroup root, boolean attachToRoot) 方法接受三個參數(shù):其中多了一個名為 attachToRoot 的布爾值參數(shù)。這個參數(shù)表示是否將新創(chuàng)建的 View 對象添加到 root 參數(shù)指定的父級視圖中。如果 attachToRoot 參數(shù)為 true,則新的 View 對象將立即添加到 root 指定的父級視圖中,而不是等到后面再次手動添加。如果為 false,則將新創(chuàng)建的 View 對象返回給調(diào)用者,并且可以在以后的代碼中使用 addView() 方法將其添加到父級視圖中。因此,區(qū)別在于是否將新創(chuàng)建的 View 對象立即添加到指定的父級視圖中。
二、三個參數(shù)的inflate方法示例
方法頭如下:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
分三種情況說明:
1、root不為null,attachToRoot為true
當(dāng)root不為null,attachToRoot為true時,表示將resource指定的布局添加到root中,添加的過程中resource所指定的的布局的根節(jié)點的各個屬性都是有效的。比如下面一個案例,Activity的布局如下:
linearlayout.xml的布局如下:
把linearlayout.xml布局文件添加到activity的布局中:
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout ll = (LinearLayout) findViewById(R.id.ll); LayoutInflater inflater = LayoutInflater.from(this); inflater.inflate(R.layout.linearlayout, ll,true);}
注意到,這里沒寫將inflate出來的View添加到ll中的代碼,但是linearlayout布局文件就已經(jīng)添加進來了,這就是因為第三個參數(shù)設(shè)置為了true,表示將名列前茅個參數(shù)所指定的布局添加到第二個參數(shù)的View中。最終顯示效果如下:
如果多寫一行代碼,如下:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout ll = (LinearLayout) findViewById(R.id.ll); LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.linearlayout, ll, true); ll.addView(view);}
這個時候再運行,系統(tǒng)會拋如下異常:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
原因就是當(dāng)?shù)谌齻€參數(shù)為true時,會自動將名列前茅個參數(shù)所指定的View添加到第二個參數(shù)所指定的View中。
2、root不為null,attachToRoot為false
如果root不為null,而attachToRoot為false的話,表示不將名列前茅個參數(shù)所指定的View添加到root中,那么,既然不添加到root中,為什么不把第二個參數(shù)直接給null?其實不然,這里涉及到另外一個問題:在開發(fā)的過程中給控件所指定的layout_width和layout_height到底是什么意思?該屬性的表示一個控件在容器中的大小,就是說這個控件必須在容器中,這個屬性才有意義,否則無意義。這就意味著如果直接將linearlayout加載進來而不給它指定一個父布局,則inflate布局的根節(jié)點的layout_width和layout_height屬性將會失效(因為這個時候linearlayout將不處于任何容器中,那么它的根節(jié)點的寬高自然會失效)。如果想讓linearlayout的根節(jié)點有效,又不想讓其處于某一個容器中,那就可以設(shè)置root不為null,而attachToRoot為false。這樣,指定root的目的也就很明確了,即root會協(xié)助linearlayout的根節(jié)點生成布局參數(shù),只有這一個作用。還是上面的布局文件,如果想將之添加到activity的布局中又該如何:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout ll = (LinearLayout) findViewById(R.id.ll); LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.linearlayout, ll, false); ll.addView(view);}
注意,這個時候需要手動的將inflate加載進來的view添加到ll容器中,因為inflate的最后一個參數(shù)false表示不將linealayout添加到ll中。顯示效果和上文一樣。
3、root為null
當(dāng)root為null時,不論attachToRoot為true還是為false,顯示效果都是一樣的。當(dāng)root為null表示不需要將名列前茅個參數(shù)所指定的布局添加到任何容器中,同時也表示沒有任何容器來來協(xié)助名列前茅個參數(shù)所指定布局的根節(jié)點生成布局參數(shù)。還是使用上文提到的linearlayout,來看下面一段代碼:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout ll = (LinearLayout) findViewById(R.id.ll); LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.linearlayout, null, false); ll.addView(view); }
當(dāng)?shù)诙€參數(shù)為null,第三個參數(shù)為false時(即使為true顯示效果也是一樣的,這里以false為例),由于在inflate方法中沒有將linearlayout添加到某一個容器中,所以需要手動添加,另外由于linearlayout并沒有處于某一個容器中,所以它的根節(jié)點的寬高屬性會失效,顯示效果如下:
這個時候不管給linearlayout的根節(jié)點的寬高設(shè)置什么,都是沒有效果的,它都是包裹button,如果修改button,則button會立即有變化,因為button是處于某一個容器中的。
三、兩個參數(shù)的inflate方法
源碼:
public View inflate(XmlPullParser parser, @Nullable ViewGroup root) { return inflate(parser, root, root != null); }
這是兩個參數(shù)的inflate方法,注意兩個參數(shù)實際上最終也是調(diào)用了三個參數(shù)。
兩個參數(shù)的inflate方法分為如下兩種情況:
root為null,等同于第二點的第3種情況。root不為null,等同于第二點的第1種情況。延伸閱讀1:為什么Activity布局的根節(jié)點的寬高屬性會生效
原因很簡單,大部分情況下一個Activity頁面由兩部分組成(Android的版本號和應(yīng)用主題會影響到Activity頁面組成,這里以常見頁面為例),頁面中有一個拔尖View叫做DecorView,DecorView中包含一個豎直方向的LinearLayout,LinearLayout由兩部分組成,名列前茅部分是標(biāo)題欄,第二部分是內(nèi)容欄,內(nèi)容欄是一個FrameLayout,在Activity中調(diào)用setContentView就是將View添加到這個FrameLayout中,所以給大家一種錯覺仿佛Activity的根布局很特殊,其實不然。