Android | 帶你探究 LayoutInflater 佈局解析原理

前言

  • 在 Android UI 開發中,經常需要用到 LayoutInflater 類,它的基本作用是將 xml 佈局文件解析成 View / View 樹。除了基本的佈局解析功能,LayoutInflater 還可以用於實現 動態換膚視圖轉換屬性轉換 等需求。
  • 在這篇文章裏,我將帶你理解 LayoutInflater 的源碼。另外,文末的應試建議也不要錯過哦,如果能幫上忙,請務必點贊加關注,這真的對我非常重要。

目錄


1. 獲取 LayoutInflater 對象

@SystemService(Context.LAYOUT_INFLATER_SERVICE)
public abstract class LayoutInflater {
    ...
}

首先,你要獲得 LayoutInflater 的實例,由於 LayoutInflater 是抽象類,不能直接創建對象,因此這裏總結一下獲取 LayoutInflater 對象的方法。具體如下:

  • 1. View.inflate(...)
public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
    LayoutInflater factory = LayoutInflater.from(context);
    return factory.inflate(resource, root);
}
  • 2. Activity#getLayoutInflater()
public LayoutInflater getLayoutInflater() {
    return getWindow().getLayoutInflater();
}
  • 3. PhoneWindow#getLayoutInflater()
private LayoutInflater mLayoutInflater;

public PhoneWindow(Context context) {
    super(context);
    mLayoutInflater = LayoutInflater.from(context);
}

public LayoutInflater getLayoutInflater() {
    return mLayoutInflater;
}
  • 4. 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;
}

可以看到,前面 3 種方法最後走到LayoutInflater#from(Context),這其實也是平時用的最多的方式。現在,我們看getSystemService(...)內的邏輯:

ContextImpl.java

@Override
public Object getSystemService(String name) {
    return SystemServiceRegistry.getSystemService(this, name);
}

----------------------------------------------------
SystemServiceRegistry.java

private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS = new ArrayMap<String, ServiceFetcher<?>>();

static {
    ...
    1. 註冊 Context.LAYOUT_INFLATER_SERVICE 與服務獲取器
    關注點:CachedServiceFetcher
    關注點:PhoneLayoutInflater
    registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class, new CachedServiceFetcher<LayoutInflater>() {
        @Override
        public LayoutInflater createService(ContextImpl ctx) {
            return new PhoneLayoutInflater(ctx.getOuterContext());
        }});
    ...
}

2. 根據 name 獲取服務對象
public static Object getSystemService(ContextImpl ctx, String name) {
    ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
    return fetcher != null ? fetcher.getService(ctx) : null;
}

註冊服務與服務獲取器
private static <T> void registerService(String serviceName, Class<T> serviceClass, ServiceFetcher<T> serviceFetcher) {
    SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
}

3. 服務獲取器創建對象
static abstract interface ServiceFetcher<T> {
    T getService(ContextImpl ctx);
}

可以看到,ContextImpl 內部通過 SystemServiceRegistry 來獲取服務對象,邏輯並不複雜:

1、靜態代碼塊註冊了 name - ServiceFetcher 的映射
2、根據 name 獲得 ServiceFetcher
3、ServiceFetcher 創建對象

ServiceFetcher 的子類有三種類型,它們的getSystemService()都是線程安全的,主要差別體現在 單例範圍,具體如下:

ServiceFetcher子類 單例範圍 描述 舉例
CachedServiceFetcher ContextImpl域 / LayoutInflater、LocationManager等(最多)
StaticServiceFetcher 進程域 / InputManager、JobScheduler等
StaticApplicationContextServiceFetcher 進程域 使用 ApplicationContext 創建服務 ConnectivityManager

對於 LayoutInflater 來說,服務獲取器是 CachedServiceFetcher 的子類,最終獲得的服務對象爲 PhoneLayoutInflater

小結:

  • 1、獲取 LayoutInflater 對象只有通過LayoutInflater.from(context),內部委派給Context#getSystemService(...),線程安全;
  • 2、使用同一個 Context 對象,獲得的 LayoutInflater 是單例;
  • 3、LayoutInflater 的實現類是 PhoneLayoutInflater。

2. inflate(...) 主流程源碼分析

上一節,我們分析了獲取 LayoutInflater 對象的過程,現在我們可以調用inflate()進行佈局解析了。LayoutInflater#inflate(...)有多個重載方法,最終都會調用到:

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
    final Resources res = getContext().getResources();
    1. 解析預編譯的佈局
    View view = tryInflatePrecompiled(resource, res, root, attachToRoot);
    if (view != null) {
        return view;
    }
    2. 構造 XmlPull 解析器 
    XmlResourceParser parser = res.getLayout(resource);
    try {
    3. 執行解析
        return inflate(parser, root, attachToRoot);
    } finally {
        parser.close();
    }
}
  1. tryInflatePrecompiled(...)是解析預編譯的佈局,我後文再說;
  2. 構造 XmlPull 解析器 XmlResourceParser
  3. 執行解析,是解析的主流程

提示: 在這裏,我剔除了與 XmlPull 相關的代碼,只保留了我們關心的邏輯:

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
    1. 結果變量
    View result = root;
    2. 最外層的標籤
    final String name = parser.getName();
    3. <merge>
    if (TAG_MERGE.equals(name)) {
        3.1 異常
        if (root == null || !attachToRoot) {
            throw new InflateException("<merge /> can be used only with a valid "
                + "ViewGroup root and attachToRoot=true");
        }
        3.2 遞歸執行解析
        rInflate(parser, root, inflaterContext, attrs, false);
    } else {
        4.1 創建最外層 View
        final View temp = createViewFromTag(root, name, inflaterContext, attrs);
        
        ViewGroup.LayoutParams params = null;

        if (root != null) {
            4.2 創建匹配的 LayoutParams
            params = root.generateLayoutParams(attrs);
            if (!attachToRoot) {
                4.3 如果 attachToRoot 爲 false,設置LayoutParams
                temp.setLayoutParams(params);
            }
        }

        5. 以 temp 爲 root,遞歸執行解析
        rInflateChildren(parser, temp, attrs, true);
        
        6. attachToRoot 爲 true,addView()
        if (root != null && attachToRoot) {
            root.addView(temp, params);
        }

        7. root 爲空 或者 attachToRoot 爲 false,返回 temp
        if (root == null || !attachToRoot) {
            result = temp;
        }
    }
    return result;
}

-> 3.2
void rInflate(XmlPullParser parser, View parent, Context context, AttributeSet attrs, boolean finishInflate) {
    while(parser 未結束) {
        if (TAG_INCLUDE.equals(name)) {
            1) <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)) {
            2) <merge>
            throw new InflateException("<merge /> must be the root element");
        } else {
            3) 創建 View 
            final View view = createViewFromTag(parent, name, context, attrs);
            final ViewGroup viewGroup = (ViewGroup) parent;
            final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
            4) 遞歸
            rInflateChildren(parser, view, attrs, true);
            5) 添加到視圖樹
            viewGroup.addView(view, params);
        }
    }
}

-> 5. 遞歸執行解析
final void rInflateChildren(XmlPullParser parser, View parent, AttributeSet attrs,
            boolean finishInflate) throws XmlPullParserException, IOException {
    rInflate(parser, parent, parent.getContext(), attrs, finishInflate);
}

關於 <include> & <merge>,我後文再說。對於參數 root & attachToRoot的不同情況,對應得到的輸出不同,我總結爲一張圖:


3. createViewFromTag():從 <tag> 到 View

第 2 節 主流程代碼中,用到了 createViewFromTag(),它負責由 <tag> 創建 View 對象:

已簡化
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs, boolean ignoreThemeAttr) {

    1. 應用 ContextThemeWrapper 以支持 android:theme
    if (!ignoreThemeAttr) {
        final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
        final int themeResId = ta.getResourceId(0, 0);
        if (themeResId != 0) {
            context = new ContextThemeWrapper(context, themeResId);
        }
        ta.recycle();
    }

    2. 先使用 Factory2 / Factory 實例化 View,相當於攔截
    View view;
    if (mFactory2 != null) {
        view = mFactory2.onCreateView(parent, name, context, attrs);
    } else if (mFactory != null) {
        view = mFactory.onCreateView(name, context, attrs);
    } else {
        view = null;
    }

    3. 使用 mPrivateFactory 實例化 View,相當於攔截
    if (view == null && mPrivateFactory != null) {
        view = mPrivateFactory.onCreateView(parent, name, context, attrs);
    }

    4. 調用自身邏輯
    if (view == null) {
        if (-1 == name.indexOf('.')) {
            4.1 <tag> 中沒有.
            view = onCreateView(parent, name, attrs);
        } else {
            4.2 <tag> 中有.
            view = createView(name, null, attrs);
        }
    }
    return view;     
}

-> 4.2 <tag> 中有.

構造器方法簽名
static final Class<?>[] mConstructorSignature = new Class[] {
            Context.class, AttributeSet.class};

緩存 View 構造器的 Map
private static final HashMap<String, Constructor<? extends View>> sConstructorMap =
            new HashMap<String, Constructor<? extends View>>();

public final View createView(String name, String prefix, AttributeSet attrs) {
    1) 緩存的構造器
    Constructor<? extends View> constructor = sConstructorMap.get(name);
    if (constructor != null && !verifyClassLoader(constructor)) {
        constructor = null;
        sConstructorMap.remove(name);
    }
    Class<? extends View> clazz = null;
    2) 新建構造器
    if (constructor == null) {
        2.1) 拼接 prefix + name 得到類全限定名
        clazz = mContext.getClassLoader().loadClass(prefix != null ? (prefix + name) : name).asSubclass(View.class);
        2.2) 創建構造器對象
        constructor = clazz.getConstructor(mConstructorSignature);
        constructor.setAccessible(true);
        2.3) 緩存到 Map
        sConstructorMap.put(name, constructor);
    }
    
    3) 實例化 View 對象
    final View view = constructor.newInstance(args);

    4) ViewStub 特殊處理
    if (view instanceof ViewStub) {
        // Use the same context when inflating ViewStub later.
        final ViewStub viewStub = (ViewStub) view;
        viewStub.setLayoutInflater(cloneInContext((Context) args[0]));
    }
    return view;
}

----------------------------------------------------

-> 4.1 <tag> 中沒有.

PhoneLayoutInflater.java

private static final String[] sClassPrefixList = {
    "android.widget.",
    "android.webkit.",
    "android.app."
};

已簡化
protected View onCreateView(String name, AttributeSet attrs) {
    for (String prefix : sClassPrefixList) {
        View view = createView(name, prefix, attrs);
            if (view != null) {
                return view;
            }
    }
    return super.onCreateView(name, attrs);
}
    1. 應用 ContextThemeWrapper 以支持android:theme,這是處理針對特定 View 設置主題;
    1. 使用 Factory2 / Factory 實例化 View,相當於攔截,我後文再說;
    1. 使用 mPrivateFactory 實例化View,相當於攔截,我後文再說;
    1. 調用 LayoutInflater 自身邏輯,分爲:
    • 4.1 <tag> 中沒有.,這是處理<linearlayout>、<TextView>等標籤,依次嘗試拼接 3 個路徑前綴,進入 3.2 實例化 View
    • 4.2 <tag> 中有.,真正實例化 View 的地方,主要分爲 4 步:
    1) 緩存的構造器
    2) 新建構造器
    3) 實例化 View 對象
    4) ViewStub 特殊處理
    

小結:

  • 使用 Factory2 接口可以攔截實例化 View 對象的步驟;
  • 實例化 View 的優先順序爲:Factory2 / Factory -> mPrivateFactory -> PhoneLayoutInflater;
  • 使用反射實例化 View 對象,同時構造器對象做了緩存;

4. Factory2 接口

現在我們來討論Factory2接口,上一節提到,Factory2可以攔截實例化 View 的步驟,在 LayoutInflater 中有兩個方法可以設置:

LayoutInflater.java

方法1:
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);
    }
}

方法2 @hide
public void setPrivateFactory(Factory2 factory) {
    if (mPrivateFactory == null) {
        mPrivateFactory = factory;
    } else {
        mPrivateFactory = new FactoryMerger(factory, factory, mPrivateFactory, mPrivateFactory);
    }
}

現在,我們來看源碼中哪裏調用這兩個方法:

4.1 setFactory2()

在 AppCompatActivity & AppCompatDialog 中,相關源碼簡化如下:

AppCompatDialog.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    設置 Factory2
    getDelegate().installViewFactory();
    super.onCreate(savedInstanceState);
    getDelegate().onCreate(savedInstanceState);
}

----------------------------------------------------

AppCompatActivity.java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    final AppCompatDelegate delegate = getDelegate();
    設置 Factory2
    delegate.installViewFactory();
    delegate.onCreate(savedInstanceState);
    夜間主題相關
    if (delegate.applyDayNight() && mThemeId != 0) {
        if (Build.VERSION.SDK_INT >= 23) {
            onApplyThemeResource(getTheme(), mThemeId, false);
        } else {
            setTheme(mThemeId);
        }
    }
    super.onCreate(savedInstanceState);
}

----------------------------------------------------

AppCompatDelegateImpl.java

public void installViewFactory() {
    LayoutInflater layoutInflater = LayoutInflater.from(mContext);
    if (layoutInflater.getFactory() == null) {
        關注點:設置 Factory2 = this(AppCompatDelegateImpl)
        LayoutInflaterCompat.setFactory2(layoutInflater, this);
    } else {
        if (!(layoutInflater.getFactory2() instanceof AppCompatDelegateImpl)) {
            Log.i(TAG, "The Activity's LayoutInflater already has a Factory installed"
                        + " so we can not install AppCompat's");
        }
    }
}

----------------------------------------------------

LayoutInflaterCompat.java

public static void setFactory2(@NonNull LayoutInflater inflater, @NonNull LayoutInflater.Factory2 factory) {
    inflater.setFactory2(factory);

    if (Build.VERSION.SDK_INT < 21) {
        final LayoutInflater.Factory f = inflater.getFactory();
        if (f instanceof LayoutInflater.Factory2) {
            forceSetFactory2(inflater, (LayoutInflater.Factory2) f);
        } else {
            forceSetFactory2(inflater, factory);
        }
    }
}

可以看到,在 AppCompatDialog & AppCompatActivity 初始化時,都通過setFactory2()設置了攔截器,設置的對象是 AppCompatDelegateImpl:

已簡化
class AppCompatDelegateImpl extends AppCompatDelegate
        implements MenuBuilder.Callback, LayoutInflater.Factory2 {

    @Override
    public final View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        return createView(parent, name, context, attrs);
    }

    @Override
    public View createView(View parent, final String name, @NonNull Context context,
            @NonNull AttributeSet attrs) {
        if (mAppCompatViewInflater == null) {
            mAppCompatViewInflater = new AppCompatViewInflater();
        }
    }
    委託給 AppCompatViewInflater 處理
    return mAppCompatViewInflater.createView(...)
}

AppCompatViewInflater 與 LayoutInflater 的核心流程差不多,主要差別是前者會<TextView>等標籤解析爲AppCompatTextView對象:

AppCompatViewInflater.java

final View createView(...) {
    ...

    switch (name) {
        case "TextView":
            view = createTextView(context, attrs);
            break;
        ...
        default:
            view = createView(context, name, attrs);
    }
    return view;
}

@NonNull
protected AppCompatTextView createTextView(Context context, AttributeSet attrs) {
    return new AppCompatTextView(context, attrs);
}

4.2 setPrivateFactory()

setPrivateFactory()是 hide 方法,在 Activity 中調用,相關源碼簡化如下:

Activity.java

final FragmentController mFragments = FragmentController.createController(new HostCallbacks());

final void attach(Context context, ActivityThread aThread,...) {
    attachBaseContext(context);
    mFragments.attachHost(null /*parent*/);

    mWindow = new PhoneWindow(this, window, activityConfigCallback);
    mWindow.setWindowControllerCallback(this);
    mWindow.setCallback(this);
    mWindow.setOnWindowDismissedCallback(this);
    關注點:設置 Factory2
    mWindow.getLayoutInflater().setPrivateFactory(this);
    ...
}

可以看到,這裏設置的 Factory2 其實就是 Activity 本身(this),這說明 Activity 也實現了 Factory2 :

public class Activity extends ContextThemeWrapper implements LayoutInflater.Factory2,...{

    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        if (!"fragment".equals(name)) {
            return onCreateView(name, context, attrs);
        }

        return mFragments.onCreateView(parent, name, context, attrs);
    }
}

原來<fragment>標籤的處理是在這裏設置的 Factory2 處理的,關於FragmentController#onCreateView(...)內部如何生成 Fragment 以及返回 View 的邏輯,我們在這篇文章裏討論,請關注:《Android | 考點爆滿,帶你全方位圖解 Fragment 源碼》

小結:

  • 使用 setFactory2() 和 setPrivateFactory() 可以設置 Factory2 接口(攔截器),其中同一個 LayoutInflater 的setFactory2()不能重複設置,setPrivateFactory() 是 hide 方法;
  • AppCompatDialog & AppCompatActivity 初始化時,調用了setFactory2(),會將一些<tag>轉換爲AppCompat版本;
  • Activity 初始化時,調用了setPrivateFactory(),用來處理<fragment>標籤。

5. <include> & <merge> & <ViewStub>

這一節,我們專門來討論<include> & <merge> & <ViewStub>的用法與注意事項:

5.1 <include> 佈局重用

5.2 <merge> 降低佈局層次

5.3 <viewstub> 佈局懶加載

Editting...


6. 總結

  • 應試建議
  1. 理解 獲取 LayoutInflater 對象的方式,知曉 3 種單例域的區別,其中 Context 域是最多的;
  2. 重點理解 LayoutInflater 佈局解析的 核心流程
  3. Factory2 是一個很實用的接口,需要掌握通過 setFactory2() 攔截佈局解析 的技巧。

推薦閱讀

感謝喜歡!你的點贊是對我最大的鼓勵!歡迎關注彭旭銳的GitHub!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章