1. 程式人生 > >Android中Inflate()常見用法說明

Android中Inflate()常見用法說明

參考文章:http://blog.csdn.net/zuolongsnail/article/details/6370035

Inflate()作用就是將xml定義的一個佈局找出來,但僅僅是找出來而且隱藏的,沒有找到的同時並顯示功能。最近做的一個專案就是這一點讓我迷茫了好幾天。

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

區別在於:

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

  1. View view1=View.inflate(this,R.layout.dialog_layout,null);  
  2.   TextViewdialogTV=(TextView)view1.findViewById(R.id.dialog_tv);  
  3.   dialogTV.setText("abcd");  
注:R.id.dialog_tv是在對話方塊layout上的元件,而這時若直接用this.findViewById(R.id.dialog_tv)肯定會報錯。
  1. View viewStub = ((ViewStub) findViewById(R.id.stubView)).inflate();  

Inflate()或可理解為“隱性膨脹”,隱性擺放在view裡,inflate()前只是獲得控制元件,但沒有大小沒有在View裡佔據空間,inflate()後有一定大小,只是出於隱藏狀態

========================================================================================

補充:

LayoutInflater的作用類似於 findViewById(),不同點是LayoutInflater是用來找layout資料夾下的xml佈局檔案,並且例項化!

而 findViewById()是找具體某一個xml下的具體 widget控制元件(如:Button,TextView等)。
獲取它的用法有3種:

方法1:

LayoutInflater inflater = LayoutInflater.from(this);
View view=inflater.inflate(R.layout.ID, null);
//或寫成:
View view=LayoutInflater.from(this).inflate(R.layout.ID, null);
方法2:由服務獲取
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
方法3:呼叫Activity的getLayoutInflater() 函式獲取LayoutInflater 物件。

================================================================================

setContentView和inflate區別

一般用LayoutInflater做一件事:inflate

inflate這個方法總共有四種形式(見下面),目的都是把xml表述的layout轉化為View物件
其中有一個比較常用,View inflate(int resource, ViewGroup root),另三個,其實目的和這個差不多。
int resource,也就是resource/layout檔案在R檔案中對應的ID,這個必須指定。
而ViewGroup root則可以是null,null時就只建立一個resource對應的View,不是null時,會將建立的view自動加為root的child。

setContentView()一旦呼叫, layout就會立刻顯示UI;而inflate只會把Layout形成一個以view類實現成的物件,有需要時再用setContentView(view)顯示出來
一般在activity中通過setContentView()將介面顯示出來,但是如果在非activity中如何對控制元件佈局設定操作了,這需LayoutInflater動態載入

< TextView
android:id="@+id/tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ATAAW.COM"
/>
< Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="按鈕"
/>

//在程式中動態載入以上佈局。
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);
//獲取佈局中的控制元件。
button = (Button) view.findViewById(R.id.button);//這裡的view為上面獲取的view物件
textView = (TextView)view.findViewById(R.id.tview);
LayoutInflater.inflate()將Layout檔案轉換為View,專門供Layout使用的Inflater。雖然Layout也是View的子類,但在android中如果想將xml中的Layout轉換為View放入.java程式碼中操作,只能通過Inflater,而不能通過findViewById()。 

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) 
不同點是LayoutInflater是用來找layout下xml佈局檔案,並且例項化!而findViewById()是找具體xml下的具體 widget控制元件(如:Button,TextView等)。