【问题标题】:Difference in inflating views膨胀视图的差异
【发布时间】:2017-02-19 23:05:52
【问题描述】:

想问。有什么区别:

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

还有:

LayoutInflater layoutInflater = LayoutInflater.from(this);

?

【问题讨论】:

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


【解决方案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

【讨论】:

    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多