天天看點

Android中inflate方法的用法總結

    在實際開發中LayoutInflater這個類還是非常有用的,它的作用類似于findViewById()。不同點是LayoutInflater是用來找res/layout/下的xml布局檔案,并且執行個體化;而findViewById()是找xml布局檔案下的具體widget控件(如Button、TextView等)。 具體作用: 1、對于一個沒有被載入或者想要動态載入的界面,都需要使用LayoutInflater.inflate()來載入; 2、對于一個已經載入的界面,就可以使用Activiyt.findViewById()方法來獲得其中的界面元素。 LayoutInflater 是一個抽象類,在文檔中如下聲明: public abstract class LayoutInflater extends Object 獲得 LayoutInflater 執行個體的三種方式 1. LayoutInflater inflater = getLayoutInflater();//調用Activity的getLayoutInflater() 2. LayoutInflater inflater = LayoutInflater.from(context); 3. LayoutInflater inflater = (LayoutInflater)context.getSystemService                               (Context.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()。 另外getSystemService()是Android很重要的一個API,它是Activity的一個方法,根據傳入的NAME來取得對應的Object,然後轉換成相應的服務對象。以下介紹系統相應的服務。   傳入的Name 傳回的對象 說明 WINDOW_SERVICE WindowManager 管理打開的視窗程式 LAYOUT_INFLATER_SERVICE LayoutInflater 取得xml裡定義的view ACTIVITY_SERVICE ActivityManager 管理應用程式的系統狀态 POWER_SERVICE PowerManger 電源的服務 ALARM_SERVICE AlarmManager 鬧鐘的服務 NOTIFICATION_SERVICE NotificationManager 狀态欄的服務 KEYGUARD_SERVICE KeyguardManager 鍵盤鎖的服務 LOCATION_SERVICE LocationManager 位置的服務,如GPS SEARCH_SERVICE SearchManager 搜尋的服務 VEBRATOR_SERVICE Vebrator 手機震動的服務 CONNECTIVITY_SERVICE Connectivity 網絡連接配接的服務 WIFI_SERVICE WifiManager Wi-Fi服務 TELEPHONY_SERVICE TeleponyManager 電話服務

inflate 方法 通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,傳回值均是 View 對象,如下: public View inflate (int resource, ViewGroup root) 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) 示意代碼: LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));

inflate方法的二種形式:

1.通過 LayoutInflater類的inflate()方法動态加載。兩種獲得LayoutInflater的方法

a. 通過SystemService獲得

1      
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE);
      

b. 從給定的context中擷取

1      
Public static LayoutInflater from(Context context)
      

c. 兩者的差別:實際上是一樣的,源碼

1
2
3
4
5
6
7
8      
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;
}
      

d. 實作代碼:

第一步,先獲得LayoutInflater對象。

1      
LayoutInflater inflater=context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE);
      

第二步,調用inflate()方法加載布局資源。

1      
View view=inflater.inflate(int resource, ViewGroup root);
      

參數1:要加載的xml資源檔案,加載出錯抛出InflateException異常。

參數2:新生成視圖的父層(?這個地方還不太了解),可設定為NULL。

第三步,将新生成的View加入到需要的View中,我們可以通過

findViewById()

方法擷取目前Acitivty中的視圖MyView,然後把新生成的view加入到Myview中。

1      
MyView.add(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
      

注意:需要指定視圖屬性。

——也可以調用inflater.inflate(int resource, ViewGroup root)方法時直接将視圖加入到父視圖中。 如:inflater.inflate(R.layout.login,MyView); 這種情況不好控制新視圖的長寬。建議使用第一種。

注: LayoutInflater.inflate()将Layout檔案轉換為View,專門供Layout使用的Inflater。雖然Layout也是 View的子類,但在android中如果想将xml中的Layout轉換為View放入.java代碼中操作,隻能通過Inflater,而不能通過 findViewById()。

第二種方法:使用View的靜态方法:

1      
static View inflate(Context context, int resource, ViewGroup root)
      

參數1:Acitivity或Application的上下文

參數2:指定的xml布局檔案

參數3:指定父元件。

同樣可以通過:

生成一個新的View,然後調用Add方法指定View的屬性并加入的父元件中。

當然也可以使用

View.inflate(this,R.layout.*,MyView)

直接加入到父元件中。

對比findViewById()方法 //EditText editText = (EditText)findViewById(R.id.content);// error EditText editText = (EditText)view.findViewById(R.id.content);

findViewById有兩種形式 :

    R.layout.xx是引用res/layout/xx.xml的布局檔案(inflate 方法),R.id.xx是引用布局檔案裡面的元件,元件的id是xx(findViewById方法)。

所有的元件id都能用R.id.xx來檢視,但是 元件不在setContentView()裡面的layout中就無法使用,Activity.findViewById()會出現空指針異常。

a. activity中的findViewById(int id)

b. View 中的findViewById(int id)

6.不同點是LayoutInflater是用來找layout下xml布局檔案,并且執行個體化!而findViewById()是找具體xml下的具體 widget控件(如:Button,TextView等)。

Android上還有一個與Inflate()類似功能的方法叫findViewById(),二者有時均可使用,但也有差別,

差別在于:

findViewById()隻能找出目前布局中的元件,即setConentView()的那個layout裡的元件.

如果你的Activity裡用到别的layout,比如對話框layout,你還要設定這個layout上的其他元件的内容,你就必須用inflate()方法先将對話框的layout找出來,然後再用findViewById()找到它上面的其它元件。例如:

1
2
3      
View view1=View.inflate(this,R.layout.dialog_layout,null);  
TextViewdialogTV=(TextView)view1.findViewById(R.id.dialog_tv);  
dialogTV.setText("abcd");  
      

注:R.id.dialog_tv是在對話框layout上的元件,而這時若直接用this.findViewById(R.id.dialog_tv)肯定會報錯。

1      
View viewStub = ((ViewStub) findViewById(R.id.stubView)).inflate();  
      

注意: ·inflate 方法與 findViewById 方法不同; ·inflater 是用來找 res/layout 下的 xml 布局檔案,并且執行個體化; ·findViewById() 是找具體 xml 布局檔案中的具體 widget 控件(如:Button、TextView 等)。

Inflate()作用就是将xml定義的一個布局找出來,但僅僅是找出來而且隐藏的,沒有找到的同時并顯示功能。而setContentView()将布局設定成目前螢幕即Activity的内容,可以直接顯示出來。

    SetContentView()一旦調用, layout就會立刻顯示UI;而inflate隻會把Layout形成一個以view類實作成的對象。有需要時再用setContentView(view)顯示出來。

注:Inflate()或可了解為“隐性膨脹”,隐性擺放在view裡,inflate()前隻是獲得控件,但沒有大小沒有在View裡占據空間,inflate()後有一定大小,隻是出于隐藏狀态。

一般在activity中通過setContentView()将界面顯示出來,但是如果在非activity中如何對控件布局設定操作了,這需LayoutInflater動态加載。

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11      
<TextView
    android:id="@+id/tview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="ATAAW.COM" />

<Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="按鈕" />
      

在程式中動态加載以上布局。(動态加載布局)

1
2      
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);
      

擷取布局中的控件。

1
2      
button = (Button) view.findViewById(R.id.button);
textView = (TextView)view.findViewById(R.id.tview);
      

參考

http://blog.csdn.net/uyu2yiyi/article/details/6324335

http://www.eaier.net/?p=107

轉載:

http://blog.sina.com.cn/s/blog_5da93c8f0100xm6n.html

補充

http://blog.csdn.net/jj120522/article/details/7905122

        我簡單解釋下:當root為null的時候,我們隻是把一個xml檔案執行個體化成View對象,傳回的就是xml對應的View.而當root不為null的時候,也就是存在parent.那麼我們将把這個xml執行個體化程View對象後,就會将這個View視圖add進其parent中.

        是以在這裡我們用的是LayoutInflater.from(context).inflate(R.layout.item, this);這樣其實就是把XML執行個體化後當作自己的一部分,這樣我們在調用此控件的時候,顯示的就是我們想要的那個視圖了(XML視圖).說到這裡大家明白了.這樣以後用的時候再也不會糊塗了.如果想詳細了解那麼請參考這篇文章:View視圖架構源碼分析之一:android是如何建立一個view.講解的那是的相當的透徹,看懂後對于以後我們開發是百利而無一害啊.(^__^)"

    在實際開發中LayoutInflater這個類還是非常有用的,它的作用類似于findViewById()。不同點是LayoutInflater是用來找res/layout/下的xml布局檔案,并且執行個體化;而findViewById()是找xml布局檔案下的具體widget控件(如Button、TextView等)。 具體作用: 1、對于一個沒有被載入或者想要動态載入的界面,都需要使用LayoutInflater.inflate()來載入; 2、對于一個已經載入的界面,就可以使用Activiyt.findViewById()方法來獲得其中的界面元素。 LayoutInflater 是一個抽象類,在文檔中如下聲明: public abstract class LayoutInflater extends Object 獲得 LayoutInflater 執行個體的三種方式 1. LayoutInflater inflater = getLayoutInflater();//調用Activity的getLayoutInflater() 2. LayoutInflater inflater = LayoutInflater.from(context); 3. LayoutInflater inflater = (LayoutInflater)context.getSystemService                               (Context.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()。 另外getSystemService()是Android很重要的一個API,它是Activity的一個方法,根據傳入的NAME來取得對應的Object,然後轉換成相應的服務對象。以下介紹系統相應的服務。   傳入的Name 傳回的對象 說明 WINDOW_SERVICE WindowManager 管理打開的視窗程式 LAYOUT_INFLATER_SERVICE LayoutInflater 取得xml裡定義的view ACTIVITY_SERVICE ActivityManager 管理應用程式的系統狀态 POWER_SERVICE PowerManger 電源的服務 ALARM_SERVICE AlarmManager 鬧鐘的服務 NOTIFICATION_SERVICE NotificationManager 狀态欄的服務 KEYGUARD_SERVICE KeyguardManager 鍵盤鎖的服務 LOCATION_SERVICE LocationManager 位置的服務,如GPS SEARCH_SERVICE SearchManager 搜尋的服務 VEBRATOR_SERVICE Vebrator 手機震動的服務 CONNECTIVITY_SERVICE Connectivity 網絡連接配接的服務 WIFI_SERVICE WifiManager Wi-Fi服務 TELEPHONY_SERVICE TeleponyManager 電話服務

inflate 方法 通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,傳回值均是 View 對象,如下: public View inflate (int resource, ViewGroup root) 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) 示意代碼: LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));

inflate方法的二種形式:

1.通過 LayoutInflater類的inflate()方法動态加載。兩種獲得LayoutInflater的方法

a. 通過SystemService獲得

1      
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE);
      

b. 從給定的context中擷取

1      
Public static LayoutInflater from(Context context)
      

c. 兩者的差別:實際上是一樣的,源碼

1
2
3
4
5
6
7
8      
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;
}
      

d. 實作代碼:

第一步,先獲得LayoutInflater對象。

1      
LayoutInflater inflater=context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE);
      

第二步,調用inflate()方法加載布局資源。

1      
View view=inflater.inflate(int resource, ViewGroup root);
      

參數1:要加載的xml資源檔案,加載出錯抛出InflateException異常。

參數2:新生成視圖的父層(?這個地方還不太了解),可設定為NULL。

第三步,将新生成的View加入到需要的View中,我們可以通過

findViewById()

方法擷取目前Acitivty中的視圖MyView,然後把新生成的view加入到Myview中。

1      
MyView.add(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
      

注意:需要指定視圖屬性。

——也可以調用inflater.inflate(int resource, ViewGroup root)方法時直接将視圖加入到父視圖中。 如:inflater.inflate(R.layout.login,MyView); 這種情況不好控制新視圖的長寬。建議使用第一種。

注: LayoutInflater.inflate()将Layout檔案轉換為View,專門供Layout使用的Inflater。雖然Layout也是 View的子類,但在android中如果想将xml中的Layout轉換為View放入.java代碼中操作,隻能通過Inflater,而不能通過 findViewById()。

第二種方法:使用View的靜态方法:

1      
static View inflate(Context context, int resource, ViewGroup root)
      

參數1:Acitivity或Application的上下文

參數2:指定的xml布局檔案

參數3:指定父元件。

同樣可以通過:

生成一個新的View,然後調用Add方法指定View的屬性并加入的父元件中。

當然也可以使用

View.inflate(this,R.layout.*,MyView)

直接加入到父元件中。

對比findViewById()方法 //EditText editText = (EditText)findViewById(R.id.content);// error EditText editText = (EditText)view.findViewById(R.id.content);

findViewById有兩種形式 :

    R.layout.xx是引用res/layout/xx.xml的布局檔案(inflate 方法),R.id.xx是引用布局檔案裡面的元件,元件的id是xx(findViewById方法)。

所有的元件id都能用R.id.xx來檢視,但是 元件不在setContentView()裡面的layout中就無法使用,Activity.findViewById()會出現空指針異常。

a. activity中的findViewById(int id)

b. View 中的findViewById(int id)

6.不同點是LayoutInflater是用來找layout下xml布局檔案,并且執行個體化!而findViewById()是找具體xml下的具體 widget控件(如:Button,TextView等)。

Android上還有一個與Inflate()類似功能的方法叫findViewById(),二者有時均可使用,但也有差別,

差別在于:

findViewById()隻能找出目前布局中的元件,即setConentView()的那個layout裡的元件.

如果你的Activity裡用到别的layout,比如對話框layout,你還要設定這個layout上的其他元件的内容,你就必須用inflate()方法先将對話框的layout找出來,然後再用findViewById()找到它上面的其它元件。例如:

1
2
3      
View view1=View.inflate(this,R.layout.dialog_layout,null);  
TextViewdialogTV=(TextView)view1.findViewById(R.id.dialog_tv);  
dialogTV.setText("abcd");  
      

注:R.id.dialog_tv是在對話框layout上的元件,而這時若直接用this.findViewById(R.id.dialog_tv)肯定會報錯。

1      
View viewStub = ((ViewStub) findViewById(R.id.stubView)).inflate();  
      

注意: ·inflate 方法與 findViewById 方法不同; ·inflater 是用來找 res/layout 下的 xml 布局檔案,并且執行個體化; ·findViewById() 是找具體 xml 布局檔案中的具體 widget 控件(如:Button、TextView 等)。

Inflate()作用就是将xml定義的一個布局找出來,但僅僅是找出來而且隐藏的,沒有找到的同時并顯示功能。而setContentView()将布局設定成目前螢幕即Activity的内容,可以直接顯示出來。

    SetContentView()一旦調用, layout就會立刻顯示UI;而inflate隻會把Layout形成一個以view類實作成的對象。有需要時再用setContentView(view)顯示出來。

注:Inflate()或可了解為“隐性膨脹”,隐性擺放在view裡,inflate()前隻是獲得控件,但沒有大小沒有在View裡占據空間,inflate()後有一定大小,隻是出于隐藏狀态。

一般在activity中通過setContentView()将界面顯示出來,但是如果在非activity中如何對控件布局設定操作了,這需LayoutInflater動态加載。

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11      
<TextView
    android:id="@+id/tview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="ATAAW.COM" />

<Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="按鈕" />
      

在程式中動态加載以上布局。(動态加載布局)

1
2      
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);
      

擷取布局中的控件。

1
2      
button = (Button) view.findViewById(R.id.button);
textView = (TextView)view.findViewById(R.id.tview);
      

參考

http://blog.csdn.net/uyu2yiyi/article/details/6324335

http://www.eaier.net/?p=107

轉載:

http://blog.sina.com.cn/s/blog_5da93c8f0100xm6n.html

補充

http://blog.csdn.net/jj120522/article/details/7905122

        我簡單解釋下:當root為null的時候,我們隻是把一個xml檔案執行個體化成View對象,傳回的就是xml對應的View.而當root不為null的時候,也就是存在parent.那麼我們将把這個xml執行個體化程View對象後,就會将這個View視圖add進其parent中.

        是以在這裡我們用的是LayoutInflater.from(context).inflate(R.layout.item, this);這樣其實就是把XML執行個體化後當作自己的一部分,這樣我們在調用此控件的時候,顯示的就是我們想要的那個視圖了(XML視圖).說到這裡大家明白了.這樣以後用的時候再也不會糊塗了.如果想詳細了解那麼請參考這篇文章:View視圖架構源碼分析之一:android是如何建立一個view.講解的那是的相當的透徹,看懂後對于以後我們開發是百利而無一害啊.(^__^)"