2017-02-19 95 views
0

想問一問。有什麼區別:充氣觀點差異

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

和:

LayoutInflater layoutInflater = LayoutInflater.from(this); 

+0

沒有區別,檢查'from'源代碼 – Selvin

+0

@Selvin,你能和我一起分享嗎? –

+0

什麼? Android是開源的,可以輕鬆找到它。 – Selvin

回答

0

差異並不大。 LayoutInflater#from(Context 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; 
    } 

所以,LayoutInflater#from內使用相同的context.getSystemService

參考:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/LayoutInflater.java#LayoutInflater.from%28android.content.Context%29