### 一.作用:
LayoutInflater作用是將layout的xml布局文件實例化為View類對象,LayoutInflater 的作用類似于 findViewById(),不同點是LayoutInflater是用來找layout文件夾下的xml布局文件,并且實例化!而 findViewById()是找具體某一個xml下的具體 widget控件(如:Button,TextView等)。
### 二.獲得 LayoutInflater 實例的三種方式
1.LayoutInflater inflater = getLayoutInflater(); //調用Activity的getLayoutInflater()
2.LayoutInflater inflater = LayoutInflater.from(this);
3.LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE);
其實,這三種方式本質是相同的,從源碼中可以看出:
**getLayoutInflater():**
Activity 的 getLayoutInflater() 方法是調用 PhoneWindow 的getLayoutInflater()方法,看一下該源代碼:
~~~
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
~~~
可以看出它其實是調用 LayoutInflater.from(context)。
LayoutInflater.from(context):
~~~
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
~~~
可以看出它其實調用 context.getSystemService()。
結論:所以這三種方式最終本質是都是調用的Context.getSystemService()。
### 三.實例化LayoutInflater之后,就要將layout的xml布局文件實例化為View類對象。
1.
~~~
LayoutInflater inflater = getLayoutInflater();
View view=inflater.inflate(R.layout.ID, null);
~~~
2.
~~~
LayoutInflater inflater = LayoutInflater.from(this);
View view=inflater.inflate(R.layout.ID, null);
~~~
3.
~~~
LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.ID, null);
~~~
### 四.inflate 方法
通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,返回值均是 View 對象,如下:
- public View inflate (int resource, ViewGroup root)? (常用)
- inflate()方法一般接收兩個參數,第一個參數就是要加載的布局id,第二個參數是指給該布局的外部再嵌套一層父布局,如果不需要就直接傳null。這樣就成功成功創建了一個布局的實例,之后再將它添加到指定的位置就可以顯示出來了。
- public View inflate (XmlPullParser parser, ViewGroup root)?
- public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)?
- public View inflate (int resource, ViewGroup root, boolean attachToRoot)
示意代碼:
~~~
<pre name="code" class="java">LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
//EditText editText = (EditText)findViewById(R.id.content);// error
EditText editText = (EditText)view.findViewById(R.id.content);
~~~
對于上面代碼,指定了第二個參數 ViewGroup root,當然你也可以設置為 null 值。
### 實例:
下面我們就通過一個非常簡單的小例子,來更加直觀地看一下LayoutInflater的用法。比如說當前有一個項目,其中MainActivity對應的布局文件叫做activity_main.xml,代碼如下所示:
~~~
<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>
~~~
這個布局文件的內容非常簡單,只有一個空的LinearLayout,里面什么控件都沒有,因此界面上應該不會顯示任何東西。
那么接下來我們再定義一個布局文件,給它取名為button_layout.xml,代碼如下所示:
~~~
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" >
</Button>
~~~
這個布局文件也非常簡單,只有一個Button按鈕而已。
現在我們要想辦法,如何通過LayoutInflater來將button_layout這個布局添加到主布局文件的LinearLayout中。根據剛剛介紹的用法,修改MainActivity中的代碼,如下所示:
~~~
public class MainActivity extends Activity {
private LinearLayout mainLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout) findViewById(R.id.main_layout);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View buttonLayout = layoutInflater.inflate(R.layout.button_layout, null);
mainLayout.addView(buttonLayout);
}
}
~~~
可以看到,這里先是獲取到了LayoutInflater的實例,然后調用它的inflate()方法來加載button_layout這個布局,最后調用LinearLayout的addView()方法將它添加到LinearLayout中。
現在可以運行一下程序,結果如下圖所示:

Button 在界面上顯示出來了!說明我們確實是借助LayoutInflater成功將button_layout這個布局添加到LinearLayout中了。 LayoutInflater技術廣泛應用于需要動態添加View的時候,比如在ScrollView和ListView中,經常都可以看到 LayoutInflater的身影。
參考:
http://www.cnblogs.com/merryjd/archive/2013/01/08/2851092.html
http://blog.csdn.net/guolin_blog/article/details/12921889
- 前言
- Appcompat_V7問題
- This Android SDK requires Android Developer Toolkit version 23.0.0 or above
- 創建Android項目不自動生成Activity,layout目錄為空
- 新建android項目gen目錄下未生成R文件
- 手機安全衛士02:splash界面ui
- 知識點:Android控件系列之Toast
- 手機安全衛士03:獲取更新的服務器配置,顯示更新對話框
- 異常處理:android.os.NetworkOnMainThreadException--多線程問題
- 知識點:Android控件系列之對話框AlertDialog.Builder
- 手機安全衛士04_01:界面(Activity)之間的切換,Activity和任務棧
- 知識點:Android控件系列之ProgressDialog與ProgressBar
- 手機安全衛士04_02:從服務器下載并安裝新版本安裝包
- 知識點:Intent
- 知識點:Adapter適配器
- 手機安全衛士05_1:程序主界面
- 手機安全衛士05_2:程序主界面,為每個條目添加事件
- 知識點:動態設置布局LayoutInflater
- 知識點:SharedPreferences
- 手機安全衛士06-手機防盜之自定義對話框
- 手機安全衛士07-手機防盜之進入限制
- 手機安全衛士08-一些布局和顯示的細節:State List
- 手機安全衛士09-手機防盜界面設置向導1
- 手機安全衛士10-設置向導之綁定SIM卡