膨胀观点的差异

时间:2017-02-19 23:05:52

标签: android layout view android-context layout-inflater

想要问。有什么区别:

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

LayoutInflater layoutInflater = LayoutInflater.from(this);

1 个答案:

答案 0 :(得分:1)

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