2012-08-13 64 views
7

我的问题是创建LayoutInflater实例的最佳方式是什么?有高效创建LayoutInflater

LayoutInflater inflater = LayoutInflater.from(context); 

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

哪个是更好的解决方案有什么区别?其他解决方案也很受欢迎。

谢谢。

回答

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

所以第二个解决方案应该比第一个解决方案有效。 – 2012-08-13 13:05:14

+0

对。谢谢。 – overbet13 2012-08-13 13:10:28