高效创建LayoutInflater

7

我的问题是创建LayoutInflater实例的最佳方法是什么?是否有任何区别?

LayoutInflater inflater = LayoutInflater.from(context);

并且

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

哪种方案更好?其他解决方案也可以。谢谢。
1个回答

10

如果您查看LayoutInflater.java源文件,您会发现。

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

1
因此,第二个解决方案应该比第一个更有效率。 - Andro Selva

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接