相信大家都用過LayoutInflater(布局填充器),今天主要說下我對(duì)inflate方法的使用理解却桶。
inflate方法有如下兩種:
public View inflate (int resource, ViewGroup root)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
查看源碼忽你,我們發(fā)現(xiàn)兩個(gè)參數(shù)的方法內(nèi)部引用了三個(gè)參數(shù)的方法
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
我們將兩個(gè)參數(shù)的方法轉(zhuǎn)化為三個(gè)參數(shù)的方法。如下:
inflater.inflate(R.layout.item, null);
—————>inflater.inflate(R.layout.item, null, null != null);
相當(dāng)于:
inflater.inflate(R.layout.item, null, false);
inflater.inflate(R.layout.item, parent);
—————>inflater.inflate(R.layout.item, parent, parent!= null);
相當(dāng)于:
inflater.inflate(R.layout.item, parent, true);
所以我們只需要研究四個(gè)方法
inflater.inflate(R.layout.item, null, true);
inflater.inflate(R.layout.item, null, false);
inflater.inflate(R.layout.item, parent, true);
inflater.inflate(R.layout.item, parent, false);
首先我先說下我對(duì)每個(gè)參數(shù)的理解
第一個(gè)參數(shù):想要添加的布局
第二個(gè)參數(shù):想要添加到哪個(gè)布局上面
???????????????????(null和有值的區(qū)別 null時(shí)第一個(gè)參數(shù)中最外層的布局大小無(wú)效确镊,有值的時(shí)候最外層的布局大小有效)
第三個(gè)參數(shù):是否直接添加到第二個(gè)參數(shù)布局上面
???????????????????(true代表layout文件填充的View會(huì)被直接添加進(jìn)parent士骤,而傳入false則代表創(chuàng)建的View會(huì)以其他方式被添加進(jìn)parent)
下面我們來對(duì)四個(gè)方法一一講解
為了更好的理解我準(zhǔn)備了一個(gè)簡(jiǎn)單的案例,我的Activity的布局如下:一個(gè)超簡(jiǎn)單的RelativeLayout 布局蕾域,我們?cè)谶@理解為父布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
我還有一個(gè)布局layout.xml如下:有一個(gè)寬度高度為200dp的LinearLayout 拷肌,LinearLayout 中有一個(gè)Button按鈕。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#ff0000">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開開心心每一天" />
</LinearLayout>
布局準(zhǔn)好了旨巷,下面進(jìn)入正題巨缘。我要把layout.xml這個(gè)布局文件添加到我的activity的布局中,我的Activity的代碼如下:
為了看到四個(gè)方法有什么不一樣采呐,我依次替換圈起來的代碼若锁,查看展示效果:
1:inflater.inflate(R.layout.item, null, true);
2:inflater.inflate(R.layout.item, null, false);
3:inflater.inflate(R.layout.item, parent, true);
使用該方法時(shí)rl.addView(view);代碼報(bào)錯(cuò),錯(cuò)誤異常如下
java.lang.IllegalStateException:
The specified child already has a parent.
You must call removeView() on the child's parent first.
意思是非法狀態(tài)異常斧吐,指定的child已經(jīng)有一個(gè)父容器了又固。你必須先用chile的父容器調(diào)用removeView()。
源碼中對(duì)第三個(gè)參數(shù)的解釋是
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.
attachToRoot傳入true代表layout文件填充的View會(huì)被直接添加進(jìn)parent煤率,而傳入false則代表創(chuàng)建的View會(huì)以其他方式被添加進(jìn)parent仰冠。
解決方法
刪除rl.addView(view);這一行代碼,然后運(yùn)行展示效果
4:inflater.inflate(R.layout.item, parent, false);
總結(jié)
????從案例中我們可以看到1涕侈、2這兩種方式運(yùn)行結(jié)果一樣沪停,第二個(gè)參數(shù)傳null會(huì)使要添加的布局視圖中根視圖的寬高設(shè)置失效煤辨,在使用這種方式的時(shí)候會(huì)造成我們無(wú)形中多加一層布局裳涛,為了使其子view顯示相應(yīng)高度。在這里不推薦使用
????3這種方式我們?cè)谑褂玫臅r(shí)候一定要注意众辨,它會(huì)使代表layout文件填充的View會(huì)被直接添加進(jìn)parent端三,如果我們使用這種方式后,在執(zhí)行addView()方法就會(huì)造成上面的錯(cuò)誤鹃彻。
推薦使用第四種方式 inflater.inflate(R.layout.item, parent, false);
以上是我對(duì)inflate方法的使用理解郊闯,有什么不對(duì)的地方,大家積極留言蛛株,我看到后會(huì)第一時(shí)間回復(fù)大家团赁,最后祝大家每天都有個(gè)好心情。