Android - LayoutInflater

LayoutInflater 是一個抽象類,在文檔中如下聲明

<span style="font-family:KaiTi_GB2312;"><span style="white-space:pre">	</span>public abstract class LayoutInflater extends Object </span>

具體作用: 

1、對於一個沒有被載入或者想要動態載入的界面,都需要使用LayoutInflater.inflate()來載入;

2、對於一個已經載入的界面,就可以使用Activiyt.findViewById()方法來獲得其中的界面元素。

LayoutInflater是用來找res/layout/下的xml佈局文件,並且實例化;而findViewById()是找xml佈局文件下的具體widget控件(如Button、TextView等)。 

獲得 LayoutInflater 實例的三種方式

1.LayoutInflater inflater = getLayoutInflater();  //調用Activity的getLayoutInflater()

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService

                                                (Context.LAYOUT_INFLATER_SERVICE);

3. LayoutInflater inflater = LayoutInflater.from(context);  

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章