Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

setContentView 與 inflate 的差異解析

編輯:關於 Android 程式設計

setContentView() 與 inflate() 經常被搞混,因為它們都跟「把 XML 變成畫面」有關。但兩者的角色完全不同:一個是「把某個 View 設為 Activity 的內容並立刻顯示」,另一個只是「把 XML 實體化成 View 物件,先拿在手上」。

inflate 在做什麼

LayoutInflater 存在的唯一目的,就是把 XML 描述的版面轉換成對應的 View 物件樹。它有四種多載形式,其中最常用的是:

 View inflate(int resource, ViewGroup root) 

兩個參數的意義:

  • resource:res/layout 下該 XML 在 R 檔案中對應的 ID,必填。
  • root:可為 null。為 null 時只產生 resource 對應的 View;不為 null 時,產生的 View 會被加到 root 之下,並套用 root 的 LayoutParams。

還有帶 attachToRoot 參數的版本。當 attachToRoot 為 true 時,inflate 完就直接掛上去了,返回值等於 root;為 false 時只回傳新建立的 View,由你決定何時掛。這是另一個常見的混淆點。

核心差異

一句話講完:

setContentView() 一旦呼叫,版面會立刻顯示在 Activity 上;inflate() 只是把 Layout 變成一個 View 物件,有需要時再用 setContentView(view) 或 addView() 呈現。

所以在 Activity 中顯示主版面用 setContentView;而在非 Activity 環境(自訂 View、Adapter、Dialog、Fragment)要動態產生版面,就得用 LayoutInflater。

動態載入版面的基本寫法

假設有一個 XML:

 <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="按鈕" /> 

在程式中動態載入並取得其中的元件:

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

// 必須透過這個 view 去找子元件,而不是 Activity.findViewById
Button button = (Button) view.findViewById(R.id.button);
TextView textView = (TextView) view.findViewById(R.id.tview); 

這裡有個關鍵:findViewById 要在哪個物件上呼叫,取決於那個 View 在哪棵樹裡。動態 inflate 出來的 view 尚未掛到 Activity 上,用 Activity.findViewById 會找不到,甚至出現空指標。

怎麼取得 LayoutInflater

兩種方式:

 // a. 從 SystemService 取
LayoutInflater inflater =
    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// b. 從 Context 靜態方法取
LayoutInflater inflater = LayoutInflater.from(context); 

看原始碼就知道兩者完全等價,from() 內部就是呼叫 getSystemService:

 /**
 * 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;
} 

差別只在 from() 多了一層 null 檢查,程式碼比較短,日常開發用 b 就好。

LayoutInflater 與 findViewById 的分工

兩者經常被拿來比較,其實不是同層次的東西:

  • LayoutInflater:讀 res/layout 下的 XML 版面檔並實例化整棵 View 樹。注意 Layout 雖然也是 View 的子類,但想把 XML 版面轉成 View 放進程式碼操作,只能透過 Inflater,不能用 findViewById。
  • findViewById:在已經存在的 View 樹中,依 ID 找出特定的 widget(Button、TextView 等)。

R.layout 與 R.id 的差別

這兩個也常搞混:

  • R.layout.xx 參照的是 res/layout/xx.xml 這個版面檔,交給 inflate 使用。
  • R.id.xx 參照的是版面檔中某個元件的 id,交給 findViewById 使用。

所有元件的 id 都能用 R.id.xx 查到,但前提是那個元件所在的版面已經被載入。若元件不在 setContentView 載入的版面中,Activity.findViewById() 就會找不到它。

動態掛到既有容器上

實務上更常見的不是整個換掉 Activity 版面,而是把 inflate 出來的 View 塞進某個容器:

 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.placeslist_linearlayout);
linearLayout.addView(place_type_text); 

三個實務提醒

  • inflate 的第二個參數盡量不要傳 null。傳 null 時系統無法套用父容器的 LayoutParams,你會得到一個「layout_width 莫名失效」的 View。在 Adapter 的 getView 中正確寫法是 inflate(res, parent, false)——傳了 parent 讓系統取得 LayoutParams,但 attachToRoot 設 false 避免立刻掛上去造成父子重複。
  • Fragment 的 onCreateView 一定要用 inflate。它回傳的就是一個 View,而 Fragment 沒有 setContentView 可用,這是兩者分工最清楚的地方。
  • Dialog 與自訂 View 同理。凡是不在 Activity 主版面上的 UI,都得自己 inflate 再決定掛到哪裡。

總結:inflate 是「生產」,setContentView 是「上架」。生產不一定上架,上架的通常是生產出來的東西。理解這個關係,動態 UI 的各種場景就不會亂了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved