而不是直接调用LayoutInflater之间的区别是什么? [英] What is the difference between calling LayoutInflater directly and not?

查看:117
本文介绍了而不是直接调用LayoutInflater之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一些教程去,而在Android文档,它说没有实例化时,它访问LayoutInflater直接。从谷歌文档示例:

I went through some tutorials, and in the Android Doc, it says not to access LayoutInflater directly when instantiating it. Example from the google Doc:

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

我经历了本教程是这个:

The tutorial I went through is this one:

LayoutInflater inflater = LayoutInflater.from(parent.getContext());

所以,我真的不明白的是不同的是除了明显的不同code什么。任何解释多少AP preciated。我认为Android的文档应该是我们遵循的一个,但我不知道这是否有差别。

So what I don't really understand is what the difference is besides the obvious different code. Any explanation much appreciated. I assume that the Android Doc should be the one we follow, but I am not sure if it makes a difference.

推荐答案

如果你打开​​了Android源代码,你可以看到LayoutInflator.from方法看起来像这样:

If you open up the Android source you can see that the LayoutInflator.from method looks like so:

/**
 * 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;
}

这意味着在你的问题中的两行code做同样的事情。不知道你阅读教程说完全,但我不认为在功能上有什么区别。使用方法是好的,因为它需要少打字,仅此而已。

That means the two lines of code in your question do the same thing. Not sure what the tutorial you read says exactly but I don't see any difference in functionality. Using the from method is nice since it requires a little less typing, that's it.

这篇关于而不是直接调用LayoutInflater之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆