有getLayoutInflater()和.getSystemService(Context.LAYOUT_INFLATER_SERVICE)之间的区别区别、getSystemService、getL

2023-09-05 10:03:04 作者:凡世中沉沦╮

简单的不的答案会平静我。 如果有任何差别那么它是什么?

Simple "No" answer will calm me. If there is any difference then what it is?

推荐答案

没有

只要在活动或窗口,调用 getLayoutInflater()有相同的背景下,会叫 getSystemService(),没有差别。

As long as the Activity or Window that calls getLayoutInflater() has the same Context that would call getSystemService(), there is no difference.

证明您可以通过跟踪 getLayoutInflater返​​回LayoutInflater()到LayoutInflater.from()你可以看到这只是一个快捷键 getSystemService()从源头code:

Proof You can trace the LayoutInflater returned by getLayoutInflater() to LayoutInflater.from() and you can see this is just a shortcut for getSystemService() from the source code:

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