LayoutInflater 获取有三种方法。
LayoutInflater mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
LayoutInfalter LayoutInfalter.from(Context contextf);
这两个函数是一样的结果:
LayoutInflater.from 的实现如下:
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;
也就是调用了上面getSystemService。但是增加了LayoutInflater是否为空的判断。
还有一个Activity的函数 getLayoutInflater() 也能够返回LayoutInflater 但是返回的Window 的getLayoutInflater() (其实就是PhoneWindow 的实例的LayoutInflater)
Activity.java:
mWindow = PolicyManager.makeNewWindow(this);
/**
* Convenience for calling
* {@link android.view.Window#getLayoutInflater}.
*/
public LayoutInflater getLayoutInflater() {
return getWindow().getLayoutInflater();
}
/**
* Retrieve the current {@link android.view.Window} for the activity.
* This can be used to directly access parts of the Window API that
* are not available through Activity/Screen.
*
* @return Window The current window, or null if the activity is not
* visual.
*/
public Window getWindow() {
return mWindow;
}