[Android] setContentView和inflater區別以及findViewById和inflater的區別

一般用LayoutInflater做一件事:就是把xml表述的layout轉化爲View對象。

inflate中比較常用的方法是,View inflate(int resource, ViewGroup root):int resource,也就是resource/layout文件在R文件中對應的ID,這個必須指定。
而ViewGroup root則可以是null,null時就只創建一個resource對應的View,不是null時,會將創建的view自動加爲root的child。

inflatersetContentView區別:
setContentView()一般用於加載顯示一個activity;而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);
textView = (TextView)view.findViewById(R.id.tview);
***********************************************************
接下來結合源碼說說inflate方法的四種形式:
inflate方法總共有四種形式,把xml表達的layout轉化爲view. This class is used to instantiate layout xml files into its corresponding view object. It is never be used directly——use getLayoutInflater() or getSystemService(String)getLayoutInflate() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up that is already hook up to the current context and correct configured for the device you are running on. 
1. Context.public abstract object getSystemService(String name) 
2. 兩種獲得LayoutInflater的方法 
a. 通過SystemService獲得 
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE); 
b. 從給定的context中獲取 
Public static LayoutInflater from(Context context) 
c. 兩者的區別:實際上是一樣的,源碼 
/** 
     * Obtains the LayoutInflater from the given 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; 

inflater和findViewById區別:
1. LayoutInflater.inflate()將Layout文件轉換爲View,專門供Layout使用的Inflater。雖然Layout也是View的子類,但在android中如果想將xml中的Layout轉換爲View放入.java代碼中操作,只能通過Inflater,而不能通過findViewById()。 
2. LinearLayout linearLayout = 
(LinearLayout) findViewById(R.id.placeslist_linearlayout); 
linearLayout.addView(place_type_text); 
3. 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) 
4.不同點是LayoutInflater是用來找layout下xml佈局文件,並且實例化!而findViewById()是找具體xml下的具體 widget控件(如:Button,TextView等)。 

發佈了50 篇原創文章 · 獲贊 10 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章