Java 在Android中获得布局充气器的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20994728/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:47:53  来源:igfitidea点击:

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

javaandroidlayout-inflater

提问by Hamzeh Soboh

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. But when I sent the context and created the inflater via the second way, it didn't!

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

采纳答案by Hamzeh Soboh

There is not much of a difference between them.

它们之间没有太大区别。

As doc says public abstract Object getSystemService (String name)

正如文档所说public abstract Object getSystemService (String name)

A LayoutInflater for inflating layout resources in this context.

用于在此上下文中膨胀布局资源的 LayoutInflater。

And for the public static LayoutInflater from (Context context)

对于来自(上下文上下文)公共静态 LayoutInflater

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) 有什么区别

回答by MathewI

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(). If it's the application object, you might have problems inflating views that contain fragments, IIRC.

唯一的区别是您使用的上下文。如果您使用的上下文LayoutInflater.fromContext()context.getSystemService(...)实际上是一个活动,则它应该等效于Activity.getLayoutInflater(). 如果是应用程序对象,您可能会在膨胀包含片段 IIRC 的视图时遇到问题。

回答by sachin10

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.

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

回答by Peter

Actually I think that the getLayoutInflater()- Method of Activity is a convenience - method.

实际上我认为getLayoutInflater()- 活动方法是一种方便的 - 方法。

Remember that Activitysubclasses Context, so all of the Methods available within Contextare also available in the ActivityClass.

请记住Activity子类Context,因此所有可用的方法在类Context中也可用Activity

Internally there will be a call to LayoutInflater.fromContext()or context.getSystemService(), so I would stick to context.getSystemServiceboth to avoid the unnecessary method call as well to clarify that I am making a call to a system service.

在内部将调用LayoutInflater.fromContext()or context.getSystemService(),因此我将context.getSystemService同时使用两者以避免不必要的方法调用,并澄清我正在调用系统服务。