LayoutInflater.inflate() 方法有一個接收三個參數(shù)的方法重載
inflate(int resource, ViewGroup root, boolean attachToRoot)
第三個參數(shù)attachToRoot可能很多人疑問了很長時間澈魄,我之前也不太理解汽烦,那么先將結(jié)論說一下称勋,稍后再做解釋
- 如果root為null才避,attachToRoot將失去作用春哨,設置任何值都沒有意義腰吟。
- 如果root不為null汰聋,attachToRoot設為true门粪,則會給加載的布局文件的指定一個父布局,即root烹困。
- 如果root不為null玄妈,attachToRoot設為false,則會將布局文件最外層的所有l(wèi)ayout屬性進行設置髓梅,當該view被添加到父view當中時拟蜻,這些layout屬性會自動生效。
- 在不設置attachToRoot參數(shù)的情況下枯饿,如果root不為null酝锅,attachToRoot參數(shù)默認為true。
直接看結(jié)論的話奢方,未必每一個人都能很容易的理解搔扁,下邊舉個例子說明
- 先寫一個activity_main.xml,這個布局文件很簡單蟋字,只有一個空的LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
- 再寫一個button_layout_1.xml稿蹲,這個布局我們加入一個Button按鈕,寬高固定鹊奖,并且外層沒有父Layout
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="50dp"
android:text="Button">
</Button>
- 最后寫一個button_layout_2.xml苛聘,加入Button按鈕,外層嵌套一個父Layout忠聚,父Layout的寬高固定
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="50dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
方法1. 如果我們用以下代碼來解析button_layout_1设哗,那么中Button所設置的寬高是不生效的
ViewGroup mainLayout = (LinearLayout) findViewById(R.id.main_layout);
View buttonLayout = LayoutInflater.from(this).inflate(R.layout.button_layout_1, null);
mainLayout.addView(buttonLayout);
方法2. 而如果我們設置了inflate方法的root參數(shù),并且attachToRoot設為true两蟀,那么Button所設置的寬高就生效了
ViewGroup mainLayout = (LinearLayout) findViewById(R.id.main_layout);
View buttonLayout = LayoutInflater.from(this).inflate(R.layout.button_layout_1, mainLayout, true);
// 不需要再addView
// mainLayout.addView(buttonLayout);
這是為什么呢网梢?
首先,layout_width和layout_height 其實是用于設置View在布局中的大小的垫竞,也就是說澎粟,View必須存在于一個布局中蛀序,之后如果將layout_width、layout_height 設置成match_parent 或 wrap_content 或 固定的數(shù)值才會生效活烙,這也是為什么這兩個屬性叫作layout_width和layout_height徐裸,而不是width和height
方法1中root設為null,Button外層又沒有父Layout啸盏,所以Button所設置的寬高并沒有生效
方法2中root設為mainLayout重贺,attachToRoot設為true,會給加載的布局文件的指定一個父布局回懦,所以Button的寬高就生效了气笙,這也就對應了最上邊的結(jié)論2
那么我們再來看button_layout_2
方法1. 設置root為null,Button的父布局RelativeLayout設置的寬高就無效
ViewGroup mainLayout = (LinearLayout) findViewById(R.id.main_layout);
View buttonLayout = LayoutInflater.from(this).inflate(R.layout.button_layout_2, null);
mainLayout.addView(buttonLayout);
方法2. 設置root設為mainLayout怯晕,attachToRoot設為false潜圃,Button的父布局RelativeLayout設置的寬高就生效了
ViewGroup mainLayout = (LinearLayout) findViewById(R.id.main_layout);
View buttonLayout = LayoutInflater.from(this).inflate(R.layout.button_layout_2, mainLayout, false);
mainLayout.addView(buttonLayout);
這也就對應了最上邊的結(jié)論3:如果root不為null,attachToRoot設為false舟茶,則會將布局文件最外層的所有l(wèi)ayout屬性進行設置谭期,當該view被添加到父view當中時,這些layout屬性會自動生效
好了吧凉,這下相信大家應該理解inflate()方法 attachToRoot參數(shù)的含義了吧隧出!