Android getLayoutInflater() 和 .getSystemService(Context.LAYOUT_INFLATER_SERVICE) 之间有什么区别吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12132370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any difference between getLayoutInflater() and .getSystemService(Context.LAYOUT_INFLATER_SERVICE)
提问by Lord_JABA
Simple "No" answer will calm me. If there is any difference then what it is?
简单的“不”回答会让我平静下来。如果有什么区别那么它是什么?
回答by Sam
No
不
As long as the Activity or Window that calls getLayoutInflater()
has the same Context that would call getSystemService()
, there is no difference.
只要调用的 Activity 或 WindowgetLayoutInflater()
具有与调用相同的 Context ,就getSystemService()
没有区别。
ProofYou 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:
证明你可以跟踪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;
}
回答by Driedshrimp
There is at least one situation that only
至少有一种情况,只有
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
must be used instead of the counterpart
必须使用代替对应物
getLayoutInflater
getLayoutInflater
That situation is in an arbitrary object class. For example, I have an instance of class call objectA. In objectA, I want to inflate a view onto the parent view (happen in ArrayAdapter that inflates customized row on the its listview.) In this case, context.getLayoutInflaterdoes not work since there is no activity or windows associated with the context. Only getSystemService(Context.LAYOUT_INFLATER_SERVICE)is appropriate then.
这种情况发生在任意对象类中。例如,我有一个类调用 objectA 的实例。在 objectA 中,我想将视图扩展到父视图上(发生在 ArrayAdapter 中,它会扩展其列表视图上的自定义行。)在这种情况下,context.getLayoutInflater不起作用,因为没有与上下文关联的活动或窗口。只有getSystemService(Context.LAYOUT_INFLATER_SERVICE)是合适的。
回答by Swayam
This is how you define a LayoutInflater.
这就是定义 LayoutInflater 的方式。
LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
And getLayoutInflater()
just gives "quick access to the LayoutInflater instance that the window retrieved from its Context" (from the documentation) by returning the LayoutInflater.
并且getLayoutInflater()
只是通过返回 LayoutInflater来“快速访问窗口从其上下文检索的 LayoutInflater 实例”(来自文档)。
Similarly, getSystemService(Context.LAYOUT_INFLATER_SERVICE)
is used to retrieve a LayoutInflater for inflating layout resources in this context.
类似地,getSystemService(Context.LAYOUT_INFLATER_SERVICE)
用于检索 LayoutInflater 以在此上下文中膨胀布局资源。
So, actually there is NOdifference between the two.
所以,实际上两者之间没有区别。
Source : Documentation
来源:文档
回答by Harshil Pansare
NO.
不。
There is no difference at all.
完全没有区别。