??在日常開發中,我經常使用LayoutInflater
將一個xml布局初始化為一個View對象,但是對它內部原理的了解卻是少之又少。今天,我們就來看看LayoutInflater
。
??本文主要內容:
LayoutInflater
創建流程。我們通過Activity
或者LayoutInflater
的from方法來創建一個對象,我們去看看這倆方法有啥區別。- View 創建流程。主要介紹
LayoutInflater
將一個xml解析成為一個View經歷的過程。setContentView
方法解析。分析了View
的創建流程,我們再來看看是怎么初始化Activity
的ContentView
。
??本文參考文章:
1. LayoutInflater的創建流程
??熟悉LayoutInflater
的同學應該都知道,創建LayoutInflater
對象有兩種方式:
- 通過
Activity
的getLayoutInflater
方法。- 通過
LayoutInflater
的from方法。
??可是這倆方法有啥區別呢?這是本節需要解答的地方。
(1).Context結構圖
??不過在此之前,我們先來了解Context的繼承類圖。
??可能有人會問,我們分析
LayoutInflater
,為什么還要去了解Context
的結構呢?這是因為LayoutInflater
本身就是系統的一個服務,是通過Context
的getSystemService
方法來獲取的。
??根據源碼我們知道,所有的系統服務都是在SystemServiceRegistry
類里面進行注冊,然后統一在ContextImpl
進行獲取,當然也包括LayoutInflater
。
(2). 兩種方法的區別
??我們通過Activity
的getLayoutInflater
方法獲取的實際上是Window
里面的LayoutInflater
對象,而Window的LayoutInflater
對象是在構造方法里面初始初始化的:
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
??此時這個Context就是Activity
的對象。所以從本質上來看,Activity
的getLayoutInflater
方法和LayoutInflater
的from方法沒有很大的區別,唯一區別的在于這個Context對象的不同。我們來看一看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;
}
??在from
方法里面調用的是Context
的getSystemService
方法,現在我們必須得了解整個Context的繼承體系。
??假設這里的Context
是Activity
,那么這里調用的就是Context
的getSystemService
方法
@Override
public Object getSystemService(String name) {
return mBase.getSystemService(name);
}
??那這里的mBase又是什么呢?從上面的類圖我們知道是ContextImpl的對象,怎么來證明呢?
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
// ······
// 1. 創建ContextImpl的對象
ContextImpl appContext = createBaseContextForActivity(r);
// ······
// 2. 調用Activity的attach方法
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window, r.configCallback);
// ······
}
//---------------Activity--------------------------
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Window window, ActivityConfigCallback activityConfigCallback) {
// 將ContextImpl傳遞給父類
attachBaseContext(context);
// ·······
}
//---------------ContextWapper---------------------
protected void attachBaseContext(Context base) {
if (mBase != null) {
throw new IllegalStateException("Base context already set");
}
mBase = base;
}
??整個調用鏈非常的清晰,分別是:ActivityThread#performLaunchActivity
-> Activity#attach
-> ContextWapper#attachBaseContext
。
??然后,我們再去看看ContextImpl
的getSystemService
方法:
@Override
public Object getSystemService(String name) {
return SystemServiceRegistry.getSystemService(this, name);
}
??最終的對象是從SystemServiceRegistry
里面獲取的。
2. View的創建流程
??LayoutInflater
是通過inflate
方法將一個xml布局解析成為一個View。我們都知道inflate
方法通常有三個參數,分別是:resource
、root
、attachToRoot
,表示的含義如下:
- resource:xml布局的id。
- root:解析成之后的View的父View,此參數只在
attachToRoot
為true才生效。- attachToRoot:決定解析出來的View是否添加到
root
上。
??有人可能會好奇,為什么需要第三個參數,這是因為我們將xml解析成View不一定立即需要添加到一個ViewGroup
中去,這是為什么呢?想一想RecyclerView
,RecyclerView
在初始化ItemView
時,不是立即將ItemView
添加進去,而是當ItemView
進入屏幕可見區域時才會添加,因為RecyclerView
有預加載機制,會加載一部分屏幕外的ItemView
。
??我們先看一下inflate方法:
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
try {
// ······
// 1. 如果是merge標簽,直接繞過merge標簽,
// 解析merge標簽下面的View。
if (TAG_MERGE.equals(name)) {
// ······
rInflate(parser, root, inflaterContext, attrs, false);
} else {
// 2.創建View
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
// ······
// 3.遞歸解析children
rInflateChildren(parser, temp, attrs, true);
if (root != null && attachToRoot) {
root.addView(temp, params);
}
if (root == null || !attachToRoot) {
result = temp;
}
}
} catch (XmlPullParserException e) {
final InflateException ie = new InflateException(e.getMessage(), e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
} catch (Exception e) {
final InflateException ie = new InflateException(parser.getPositionDescription()
+ ": " + e.getMessage(), e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
}
return result;
}
}
??inflate
方法里面一種做了2件事:
- 如果根View是merge,直接遞歸解析它的子View。
- 如果根View不是merge,先解析根View,然后在遞歸解析它所有的child。
??我們分為兩步來看,先看一下解析根標簽。
(1). 根View的解析
??根View的解析與child的解析不一樣,是通過createViewFromTag
方法來完成的:
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
boolean ignoreThemeAttr) {
// ······
View view;
// 1. 如果mFactory2不為空,優先讓mFactory2處理
if (mFactory2 != null) {
view = mFactory2.onCreateView(parent, name, context, attrs);
} else if (mFactory != null) {
view = mFactory.onCreateView(name, context, attrs);
} else {
view = null;
}
// 2. 如果上面解析為空,再使用mPrivateFactory常識這解析
if (view == null && mPrivateFactory != null) {
view = mPrivateFactory.onCreateView(parent, name, context, attrs);
}
if (view == null) {
final Object lastContext = mConstructorArgs[0];
mConstructorArgs[0] = context;
try {
// 如果是系統widget包下的控件
if (-1 == name.indexOf('.')) {
view = onCreateView(parent, name, attrs);
} else { // 如果是第三方包或者自定義的控件
view = createView(name, null, attrs);
}
} finally {
mConstructorArgs[0] = lastContext;
}
}
return view;
// ······
}
??inflate
方法里面主要做了三件事:
- 首先使用
mFactory2
和mFactory
來嘗試著創建View對象。mFactory2
和mFactory
二者有且只能有一個有值,所以只需要調用其中一個就行了。- 如果第一步中的兩個工廠都無法創建View對象,再嘗試著使用
mPrivateFactory
對象創建。不過通常來說,這個對象都是為空的。- 最后一步就是走兜底邏輯。這里的兜底有一點的特殊:如果View是
widget
的控件,會先在前面加一個android.wiget.
的前綴,再行創建View;其次,如果是其他包下的控件,比如說,androidX和自定義的控件,就直接創建View對象。
??關于第一點,我還想介紹一下,Google爸爸之所以要設計兩個工廠類,主要有3個方面的考慮:
- 兼容性,后面發布的版本可以兼容之前的版本,比如說,
AppCompatActivity
是新推出來的組件,所以在新版本上使用的mFactory2
,舊版本就走原來的原來邏輯,也就是默認的onCreateView
方法。- 擴展性,如果開發者需要自定義一種全局的樣式或者手動創建解析View,可以直接給
LayoutInflayer
設置Factory
,用來達到自己的目的。- 提升性能,這一點可以從可以從
AppCompatActivity
里面看出。AppCompatActivity
的內部給LayoutInflayer
設置了一個Factory2
,也就是AppCompatDelegateImpl
對象。AppCompatDelegateImpl
在解析xml時,會先判斷當前View是否基礎控件,比如說,Button
、TextView
或者ImageView
等,如果是的話,可以通過new 的方式創建對應的AppCompatXXX
對象。之所以說它提升性能,是因為它在解析基礎控件時,不再通過反射,而是通過new的方式創建的。
??上面的第三點可能有點復雜,我們可以直接看一下AppCompatDelegateImpl
的createView
方法。由于createView
內部調用了AppCompatViewInflater
的createView
方法,所以這里我們直接看AppCompatViewInflater
的createView
方法:
final View createView(View parent, final String name, @NonNull Context context,
@NonNull AttributeSet attrs, boolean inheritContext,
boolean readAndroidTheme, boolean readAppTheme, boolean wrapContext) {
final Context originalContext = context;
// ······
switch (name) {
case "TextView":
view = createTextView(context, attrs);
verifyNotNull(view, name);
break;
case "ImageView":
view = createImageView(context, attrs);
verifyNotNull(view, name);
break;
case "Button":
view = createButton(context, attrs);
verifyNotNull(view, name);
break;
case "EditText":
view = createEditText(context, attrs);
verifyNotNull(view, name);
break;
case "Spinner":
view = createSpinner(context, attrs);
verifyNotNull(view, name);
break;
case "ImageButton":
view = createImageButton(context, attrs);
verifyNotNull(view, name);
break;
case "CheckBox":
view = createCheckBox(context, attrs);
verifyNotNull(view, name);
break;
case "RadioButton":
view = createRadioButton(context, attrs);
verifyNotNull(view, name);
break;
case "CheckedTextView":
view = createCheckedTextView(context, attrs);
verifyNotNull(view, name);
break;
case "AutoCompleteTextView":
view = createAutoCompleteTextView(context, attrs);
verifyNotNull(view, name);
break;
case "MultiAutoCompleteTextView":
view = createMultiAutoCompleteTextView(context, attrs);
verifyNotNull(view, name);
break;
case "RatingBar":
view = createRatingBar(context, attrs);
verifyNotNull(view, name);
break;
case "SeekBar":
view = createSeekBar(context, attrs);
verifyNotNull(view, name);
break;
default:
view = createView(context, name, attrs);
}
// ······
return view;
}
??在默認的情況下,創建View對象的真正操作在createView
方法里面,我們可以來看看:
public final View createView(String name, String prefix, AttributeSet attrs)
throws ClassNotFoundException, InflateException {
// ······
// 如果緩存中沒有View的構造方法對象,
// 那么就創建一個,并且放入緩存中去。
if (constructor == null) {
clazz = mContext.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
if (mFilter != null && clazz != null) {
boolean allowed = mFilter.onLoadClass(clazz);
if (!allowed) {
failNotAllowed(name, prefix, attrs);
}
}
constructor = clazz.getConstructor(mConstructorSignature);
constructor.setAccessible(true);
sConstructorMap.put(name, constructor);
} else {
if (mFilter != null) {
Boolean allowedState = mFilterMap.get(name);
if (allowedState == null) {
// New class -- remember whether it is allowed
clazz = mContext.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
boolean allowed = clazz != null && mFilter.onLoadClass(clazz);
mFilterMap.put(name, allowed);
if (!allowed) {
failNotAllowed(name, prefix, attrs);
}
} else if (allowedState.equals(Boolean.FALSE)) {
failNotAllowed(name, prefix, attrs);
}
}
}
Object lastContext = mConstructorArgs[0];
if (mConstructorArgs[0] == null) {
mConstructorArgs[0] = mContext;
}
Object[] args = mConstructorArgs;
args[1] = attrs;
final View view = constructor.newInstance(args);
if (view instanceof ViewStub) {
final ViewStub viewStub = (ViewStub) view;
viewStub.setLayoutInflater(cloneInContext((Context) args[0]));
}
mConstructorArgs[0] = lastContext;
return view;
// ······
}
??從這里,我們就可以知道,LayoutInflater
創建View的本質就是Java反射,所以在我們日常開發過程中,盡量不要套太深的布局,畢竟反射的性能是有目共睹的。
(2). children的解析
??children的解析實際上是在rInflate
方法里面進行的,我們直接來看源碼:
void rInflate(XmlPullParser parser, View parent, Context context,
AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {
// ······
while (((type = parser.next()) != XmlPullParser.END_TAG ||
parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}
final String name = parser.getName();
// requestFocus標簽
if (TAG_REQUEST_FOCUS.equals(name)) {
pendingRequestFocus = true;
consumeChildElements(parser);
} else if (TAG_TAG.equals(name)) { // tag標簽
parseViewTag(parser, parent, attrs);
} else if (TAG_INCLUDE.equals(name)) { // include標簽
if (parser.getDepth() == 0) {
throw new InflateException("<include /> cannot be the root element");
}
parseInclude(parser, context, parent, attrs);
} else if (TAG_MERGE.equals(name)) { // merge標簽
throw new InflateException("<merge /> must be the root element");
} else { // 正常View或者ViewGroup
final View view = createViewFromTag(parent, name, context, attrs);
final ViewGroup viewGroup = (ViewGroup) parent;
final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
rInflateChildren(parser, view, attrs, true);
viewGroup.addView(view, params);
}
}
// ······
}
??children的遍歷就像是一個樹的遍歷,就是一種廣搜的思想,這里就不過多的討論了。
3. AppCompatActivity的setContentView方法解析
??說完了上面的原理,最后我們在來看看AppCompatActivity
的setContentView
方法。在LayoutInflater
方面,AppCompatActivity
相比于Activity
,給LayoutInflater
設置了一個Factory2
,也就是上面討論的東西。
??這里我們不再討論之前談論過的東西,而是看一個有趣的東西,我也不知道Google爸爸是怎么想的。
??AppCompatActivity
的onCreate
方法內部會給LayoutInflater
設置一個Factory2
對象,整個調用鏈是:AppCompatActivity#onCreate
-> AppCompatDelegateImpl#installViewFactory
-> LayoutInflaterCompat#setFactory2
-> LayoutInflaterCompat#forceSetFactory2
。我們直接來看setFactory2
方法:
private static void forceSetFactory2(LayoutInflater inflater, LayoutInflater.Factory2 factory) {
if (!sCheckedField) {
try {
sLayoutInflaterFactory2Field = LayoutInflater.class.getDeclaredField("mFactory2");
sLayoutInflaterFactory2Field.setAccessible(true);
} catch (NoSuchFieldException e) {
Log.e(TAG, "forceSetFactory2 Could not find field 'mFactory2' on class "
+ LayoutInflater.class.getName()
+ "; inflation may have unexpected results.", e);
}
sCheckedField = true;
}
if (sLayoutInflaterFactory2Field != null) {
try {
sLayoutInflaterFactory2Field.set(inflater, factory);
} catch (IllegalAccessException e) {
Log.e(TAG, "forceSetFactory2 could not set the Factory2 on LayoutInflater "
+ inflater + "; inflation may have unexpected results.", e);
}
}
}
??看到這個神奇操作沒?萬萬沒想到Google是通過反射的方式來給mFactory2
方法。爸爸為啥要這樣做呢?我猜測是setFactory2
方法的坑,我們來看看:
public void setFactory2(Factory2 factory) {
if (mFactorySet) {
throw new IllegalStateException("A factory has already been set on this LayoutInflater");
}
if (factory == null) {
throw new NullPointerException("Given factory can not be null");
}
mFactorySet = true;
if (mFactory == null) {
mFactory = mFactory2 = factory;
} else {
mFactory = mFactory2 = new FactoryMerger(factory, factory, mFactory, mFactory2);
}
}
??只要Factory
被設置過,不論是Factory
還是Factory2
,都不允許被再次設置。所以,我猜測是,爸爸為了成功給mFactory2
設置上值,通過反射來繞開這種限制,這也是在是無奈。
??設置了Factory2
工廠類之后,就是調用setContentView
方法來給Activity
設置ContentView
。我們這里直接來看一下AppCompatDelegateImpl
的setContentView
方法:
@Override
public void setContentView(int resId) {
ensureSubDecor();
ViewGroup contentParent = (ViewGroup) mSubDecor.findViewById(android.R.id.content);
contentParent.removeAllViews();
LayoutInflater.from(mContext).inflate(resId, contentParent);
mOriginalWindowCallback.onContentChanged();
}
??從這里我們可以看出來,contentView是通過LayoutInflater
加載出來的。具體的細節就不再討論了,上面已經詳細的分析過了。
4. 總結
??到此為止,本文算是為止。總的來說,本文還是簡單的(隱約的感覺到,本文有點水),在這里,我們對本文的內容做一個簡單的總結。
Activity
的getLayoutInflater
方法和LayoutInflater
在本質沒有任何的區別,最終都會調用到ContextImpl
的getSystemService
方法里面去。LayoutInflater
初始化View分為三步:1.調用mFactory2
或者mFactory
方法來解析xml;2. 通過mPrivateFactory
來解析xml;3. 通過onCreateView
或者createView
方法來解析xml。除了在AppCompatDelegateImpl
在解析基礎控件時使用的是new方式,其余幾乎都是反射方式來創建View,所以在布局中盡可能的少寫View或者盡量不要書寫層次很深的布局。