三個案例帶你看懂LayoutInflater中inflate方法兩個引數和三個引數的區別
關於inflate引數問題,我想很多人多多少少都瞭解一點,網上也有很多關於這方面介紹的文章,但是枯燥的理論或者翻譯讓很多小夥伴看完之後還是一臉懵逼,so,我今天想通過三個案例來讓小夥伴徹底的搞清楚這個東東。本篇部落格我們不講原始碼,只看使用。原始碼的解讀會在下一篇博文中帶來。
inflate方法從大範圍來看,分兩種,三個引數的構造方法和兩個引數的構造方法。在這兩類中又有細分,OK,那我們就把各種情況都來演示一遍。
1.三個引數的inflate方法
方法頭如下:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
好,這裡主要分為三種情況,分別來看
1.1 root不為null,attachToRoot為true
當root不為null,attachToRoot為true時,表示將resource指定的佈局新增到root中,新增的過程中resource所指定的的佈局的根節點的各個屬性都是有效的。比如下面一個案例,我的Activity的佈局如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/ll" tools:context="org.sang.layoutinflater.MainActivity"> </LinearLayout>
我還有一個佈局linearlayout.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="200dp" android:layout_height="200dp" android:background="@color/colorPrimary" android:gravity="center" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
我現在想把這個linearlayout.xml佈局檔案新增到我的activity的佈局中,那麼我可以這麼做:
@Override
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);
inflater.inflate(R.layout.linearlayout, ll,true);
}
小夥伴們注意到,這裡我都沒寫將inflate出來的View新增到ll中的程式碼,但是linearlayout佈局檔案就已經新增進來了,這就是因為我第三個引數設定為了true,表示將第一個引數所指定的佈局新增到第二個引數的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);
}
這個時候再執行,系統會拋如下異常:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
原因就是因為當第三個引數為true時,會自動將第一個引數所指定的View新增到第二個引數所指定的View中。
1.2 root不為null,attachToRoot為false
如果root不為null,而attachToRoot為false的話,表示不將第一個引數所指定的View新增到root中,那麼這個時候有的小夥伴可能就有疑問了,既然不新增到root中,那我還寫這麼多幹嘛?我第二個引數直接給null不就可以了?其實不然,這裡涉及到另外一個問題:我們在開發的過程中給控制元件所指定的layout_width和layout_height到底是什麼意思?該屬性的表示一個控制元件在容器中的大小,就是說這個控制元件必須在容器中,這個屬性才有意義,否則無意義。這就意味著如果我直接將linearlayout載入進來而不給它指定一個父佈局,則inflate佈局的根節點的layout_width和layout_height屬性將會失效(因為這個時候linearlayout將不處於任何容器中,那麼它的根節點的寬高自然會失效)。如果我想讓linearlayout的根節點有效,又不想讓其處於某一個容器中,那我就可以設定root不為null,而attachToRoot為false。這樣,指定root的目的也就很明確了,即root會協助linearlayout的根節點生成佈局引數,只有這一個作用。OK,還是上面的佈局檔案,如果我想將之新增到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的最後一個引數false表示不將linealayout新增到ll中。顯示效果和上文一樣,不再貼圖。
1.3 root為null
當root為null時,不論attachToRoot為true還是為false,顯示效果都是一樣的。當root為null表示我不需要將第一個引數所指定的佈局新增到任何容器中,同時也表示沒有任何容器來來協助第一個引數所指定佈局的根節點生成佈局引數。我還是使用上文提到的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);
}
當第二個引數為null,第三個引數為false時(即使為true顯示效果也是一樣的,這裡以false為例),由於在inflate方法中沒有將linearlayout新增到某一個容器中,所以我需要手動新增,另外由於linearlayout並沒有處於某一個容器中,所以它的根節點的寬高屬性會失效,顯示效果如下:
小夥伴們注意,這個時候不管我給linearlayout的根節點的寬高設定什麼,都是沒有效果的,它都是包裹button,如果我修改button,則button會立即有變化,因為button是處於某一個容器中的。
2.兩個引數的inflate方法
兩個引數的inflate方法就很簡單了,我們來稍微看一點點原始碼:public View inflate(XmlPullParser parser, @Nullable ViewGroup root) {
return inflate(parser, root, root != null);
}
這是兩個引數的inflate方法,大家注意兩個引數實際上最終也是呼叫了三個引數。兩個引數的inflate方法分為如下兩種情況:1.root為null,等同於1.3所述情況。2.root不為null,等同於1.1所述情況。
3.為什麼Activity佈局的根節點的寬高屬性會生效?
inflate方法我們已經說完了,小夥伴們可能有另外一個疑問,那為什麼Activity佈局的根節點的寬高屬性會生效?其實原因很簡單,大部分情況下我們一個Activity頁面由兩部分組成(Android的版本號和應用主題會影響到Activity頁面組成,這裡以常見頁面為例),我們的頁面中有一個頂級View叫做DecorView,DecorView中包含一個豎直方向的LinearLayout,LinearLayout由兩部分組成,第一部分是標題欄,第二部分是內容欄,內容欄是一個FrameLayout,我們在Activity中呼叫setContentView就是將View新增到這個FrameLayout中,所以給大家一種錯覺彷彿Activity的根佈局很特殊,其實不然。
OK,以上就是對LayoutInflater中inflate方法的一個簡單介紹,希望能夠幫助到還沒弄懂這個的小夥伴。
以上。