调用LayoutInflater直接而不是有什么区别?

我经历了一些教程,在 Android文档中,它表示在实例化时不直接访问LayoutInflater.来自Google Doc的示例:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);

我经历的教程是这样的:

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

所以我不太明白的是除了明显不同的代码之外还有什么区别.任何解释非常感谢.我认为Android文档应该是我们遵循的,但我不知道是否有所作为.

解决方法

如果您打开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;
}

这意味着你的问题中的两行代码做同样的事情.不知道你阅读的教程是什么,但是我没有看到任何功能的差异.使用from方法是很好的,因为它需要一点点打字,就是这样.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...