2012-08-13 61 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