有getLayoutInflater()和.getSystemService(Context.LAY

2019-06-27 10:03发布

简单的“不”的答案将我冷静。 如果说有什么区别那么它是什么呢?

Answer 1:

没有

只要调用活动或窗口getLayoutInflater()有需要调用相同的上下文getSystemService()没有任何区别。


证明您可以跟踪返回的LayoutInflater getLayoutInflater()来LayoutInflater.from() ,你可以看到这只是一个快捷方式getSystemService()从源代码:

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


Answer 2:

至少有一种情况,只有

getSystemService(Context.LAYOUT_INFLATER_SERVICE);

必须改用对方的

getLayoutInflater

这种情况是在任意对象类。 例如,我有类调用对象A的一个实例。 在对象A,我要一个膨胀到视图父视图(发生在ArrayAdapter该膨胀在其列表视图定制列)。在这种情况下,context.getLayoutInflater不起作用,因为没有与上下文相关的活动或窗口。 只有getSystemService(Context.LAYOUT_INFLATER_SERVICE)是合适的呢。



Answer 3:

这是你如何定义一个LayoutInflater。

LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);

getLayoutInflater()只给出了“快速访问LayoutInflater实例,从它的上下文检索窗口”(从文件通过返回LayoutInflater)。

类似地, getSystemService(Context.LAYOUT_INFLATER_SERVICE)被用来检索在此上下文中充气布局资源LayoutInflater。

因此,实际上有两个没有什么区别。

来源: 文档



Answer 4:

没有。

有没有什么不同。



文章来源: Is there any difference between getLayoutInflater() and .getSystemService(Context.LAYOUT_INFLATER_SERVICE)