2017-02-19 50 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