简体   繁体   English

在Android中获取布局inflater的正确方法是什么?

[英]What is the correct way to get layout inflater in Android?

There is a way to get layoutInflater: 有一种方法可以获得layoutInflater:

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

and another way is: 另一种方式是:

LayoutInflater inflater = LayoutInflater.from(context);

a third one (when I am in an Activity) is: 第三个(当我参加活动时)是:

LayoutInflater inflater = getLayoutInflater();

So what is the difference between them? 那么他们之间有什么区别?

Note that when I sent the third inflater to my adapter, my application worked. 请注意,当我将第三个inflater发送到我的适配器时,我的应用程序工作。 But when I sent the context and created the inflater via the second way, it didn't! 但是当我发送上下文并通过第二种方式创建了inflater时,它没有!

use outside of your activity 在您的活动之外使用

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

Within your activity 在你的活动中

     LayoutInflater inflater = getLayoutInflater();

Check this 检查一下

If you open up the Android source you can see that the LayoutInflator.from method looks like so: 如果您打开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;
    }

and there is no difference 而且没有区别

As long as the Activity or Window that calls getLayoutInflater() has the same Context that would call getSystemService() , there is no difference. 只要调用getLayoutInflater()的Activity或Window具有调用getSystemService()的相同Context,就没有区别。

There is not much of a difference between them. 它们之间没有太大区别。

As doc says public abstract Object getSystemService (String name) 正如doc所说公共抽象Object getSystemService(String name)

A LayoutInflater for inflating layout resources in this context. LayoutInflater用于在此上下文中展开布局资源。

And for the public static LayoutInflater from (Context context) 而对于公共静态LayoutInflater来自(Context context)

Obtains the LayoutInflater from the given context. 从给定的上下文中获取LayoutInflater。

You can check this thread Is there any difference between getLayoutInflater() and .getSystemService(Context.LAYOUT_INFLATER_SERVICE) 你可以查看这个帖子getLayoutInflater()和.getSystemService(Context.LAYOUT_INFLATER_SERVICE)之间有什么区别吗

The only difference is the context that you use. 唯一的区别是您使用的上下文。 If the context that you use with LayoutInflater.fromContext() or context.getSystemService(...) is actually an Activity, it should be equivalent to Activity.getLayoutInflater() . 如果与LayoutInflater.fromContext()context.getSystemService(...)一起使用的上下文实际上是一个Activity,则它应该等同于Activity.getLayoutInflater() If it's the application object, you might have problems inflating views that contain fragments, IIRC. 如果它是应用程序对象,则可能会出现包含片段IIRC的视图的问题。

Actually I think that the getLayoutInflater() - Method of Activity is a convenience - method. 实际上我认为getLayoutInflater() - Activity of Activity是一种方便的方法。

Remember that Activity subclasses Context , so all of the Methods available within Context are also available in the Activity Class. 请记住, Activity子类是Context ,因此Context中可用的所有方法也可以在Activity类中使用。

Internally there will be a call to LayoutInflater.fromContext() or context.getSystemService() , so I would stick to context.getSystemService both to avoid the unnecessary method call as well to clarify that I am making a call to a system service. 在内部会有一个对LayoutInflater.fromContext()context.getSystemService()的调用,所以我会坚持使用context.getSystemService来避免不必要的方法调用以澄清我正在调用系统服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM