gpt4 book ai didi

android - getLayoutInflater 与 LayoutInflater.from

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:04 29 4
gpt4 key购买 nike

研究一些(已知是好的)代码我可以看到逻辑如下:

if (getContext() instanceof Activity) {
inflater=((Activity)getContext()).getLayoutInflater();
}
else {
inflater=LayoutInflater.from(getContext());
}

我想知道,为什么这个 if/else,在所有情况下只使用 LayoutInflater.from 有什么好处?

最佳答案

这并不重要。

Activity delegates getLayouInflater() to Window . WindowPhoneWindow的常用策略依次执行initializes its inflater with LayoutInflater.from(context)其中 context 是 Activity 。

所以 inflater 对象实际上是相同的,在 Activity 的情况下使用相同的 Context

LayoutInflater.from() 实际上是一个 wrapper for Context.getSystemService() .现在,系统服务是looked up by name from a map然后 retrieved from a cache .与访问 Activity 中已初始化的成员变量相比,此查找有一些开销。

因此,与实际的 View 层次结构膨胀相比,它看起来像是微优化,不太可能影响太多运行时性能。

这种优化实际上会对开发人员的工作效率产生负面影响,因为人们需要停下来思考一下代码为何存在。

关于android - getLayoutInflater 与 LayoutInflater.from,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26647277/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com