gpt4 book ai didi

java - 在 Android 中获取 LayoutInflater 的正确方法是什么?

转载 作者:IT老高 更新时间:2023-10-28 21:06:30 25 4
gpt4 key购买 nike

有一种方法可以得到layoutInflater:

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

另一种方式是:

LayoutInflater inflater = LayoutInflater.from(context);

第三个(当我在一个 Activity 中)是:

LayoutInflater inflater = getLayoutInflater();

那么它们之间有什么区别呢?

请注意,当我将第三个充气机发送到我的适配器时,我的应用程序正常工作。但是当我通过第二种方式发送上下文并创建充气器时,它没有!

最佳答案

在您的 Activity 之外使用

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

在你的 Activity 中

     LayoutInflater inflater = getLayoutInflater();

Check this

如果你打开 Android 源代码,你可以看到 LayoutInflator.from 方法如下所示:

    /**
* Obtains the LayoutInflater from the given context.
*/
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;
}

没有区别

只要调用 getLayoutInflater() 的 Activity 或 Window 与调用 getSystemService() 的 Context 相同,就没有区别。

关于java - 在 Android 中获取 LayoutInflater 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20994728/

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