充气视图的差异

Difference in inflating views

想问一下。有什么区别:

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

并且:

LayoutInflater layoutInflater = LayoutInflater.from(this);

?

差别不大。 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