简单的“不”回答会让我平静下来。如果有任何区别,那么它是什么?
4 回答
不
只要调用的 Activity 或 Window 与调用getLayoutInflater()
的 Context 相同,就getSystemService()
没有区别。
证明您可以跟踪由LayoutInflater.from()getLayoutInflater()
返回的LayoutInflater ,您可以看到这只是源代码的快捷方式: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;
}
至少有一种情况只有
获取系统服务(上下文。LAYOUT_INFLATER_SERVICE);
必须使用而不是对应的
getLayoutInflater
这种情况是在任意对象类中。例如,我有一个类调用 objectA 的实例。在 objectA 中,我想将视图膨胀到父视图上(发生在 ArrayAdapter 中,它会膨胀其列表视图上的自定义行。)在这种情况下,context.getLayoutInflater不起作用,因为没有与上下文关联的活动或窗口。只有getSystemService(Context.LAYOUT_INFLATER_SERVICE)是合适的。
这就是您定义 LayoutInflater 的方式。
LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
并且getLayoutInflater()
只是通过返回 LayoutInflater 来“快速访问窗口从其上下文中检索的 LayoutInflater 实例”(来自文档)。
同样,getSystemService(Context.LAYOUT_INFLATER_SERVICE)
用于检索 LayoutInflater 以在此上下文中膨胀布局资源。
因此,实际上两者之间没有区别。
来源:文档
不。
完全没有区别。