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

Android LayoutInflater 用法詳解:inflate 與 findViewById 的差別

編輯:關於 Android 程式設計

實際開發中,LayoutInflater 是個出場率極高卻常被誤解的類別。它的作用有點像 findViewById(),兩者卻不是同一回事:LayoutInflater 負責找出 res/layout/ 底下的 XML 版面檔並把它「實體化」成 View 物件;findViewById() 則是在已經存在的 View 樹中,找出某個具體的子元件。

什麼時候用哪一個

· 若版面尚未被載入,或需要動態載入,用 LayoutInflater.inflate()。
· 若版面已經載入(例如 setContentView() 之後),用 Activity.findViewById() 取得其中的元件。

一句話分辨:inflate 是「把 XML 變成 View」,findViewById 是「在 View 裡找 View」。

LayoutInflater 本身是抽象類別,官方宣告如下:

public abstract class LayoutInflater extends Object

取得實例的三種方式

// 1. 呼叫 Activity 的方法
LayoutInflater inflater = getLayoutInflater();

// 2. 用靜態方法
LayoutInflater inflater = LayoutInflater.from(context);

// 3. 直接從系統服務取得
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

這三種寫法看起來不同,本質卻完全一樣。從原始碼可以看出端倪:

getLayoutInflater()
Activity 的這個方法其實是轉呼叫 PhoneWindow 的版本,而 PhoneWindow 在 constructor 中這樣寫:

public PhoneWindow(Context context) {
    super(context);
    mLayoutInflater = 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()。

順帶認識 getSystemService()

這個 API 是 Activity 相當重要的一個方法,依傳入的 NAME 取得對應物件並轉型成該服務。常用的服務對照如下:

傳入的 Name回傳的物件用途
WINDOW_SERVICEWindowManager管理已開啟的視窗
LAYOUT_INFLATER_SERVICELayoutInflater取得 XML 中定義的 View
ACTIVITY_SERVICEActivityManager管理應用程序的系統狀態
POWER_SERVICEPowerManager電源相關服務
ALARM_SERVICEAlarmManager鬧鐘服務
NOTIFICATION_SERVICENotificationManager狀態列通知服務
KEYGUARD_SERVICEKeyguardManager鍵盤鎖服務
LOCATION_SERVICELocationManager位置服務,如 GPS
SEARCH_SERVICESearchManager搜尋服務
VIBRATOR_SERVICEVibrator手機震動服務
CONNECTIVITY_SERVICEConnectivityManager網路連線服務
WIFI_SERVICEWifiManagerWi-Fi 服務
TELEPHONY_SERVICETelephonyManager電話服務

inflate 方法的四種形式

依 SDK 文件,這個方法的重載版本如下,回傳值都是 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));
// 錯誤寫法:EditText editText = (EditText) findViewById(R.id.content);
// 必須從 inflate 出來的 view 上找
EditText editText = (EditText) view.findViewById(R.id.content);

第二個參數 root 也可以傳 null,但這會失去版面參數(layout_width / layout_height)的解析,實務上盡量傳入真正的父容器。

重點回顧

· inflate 與 findViewById 是兩件不同的事。
· inflate 找出 res/layout 下的 XML 並將其實例化。
· findViewById 是在既有的版面樹中找特定元件。
· 三種取得 LayoutInflater 的方式本質相同,都是走 getSystemService()。

理解這個分工之後,自訂控制元件、Adapter 的 getView()、Dialog 的自訂版面等常見場景,就不容易再搞混該用哪一個了。

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