android - 充气 View 的差异

标签 android layout view android-context layout-inflater

想问一下。有什么区别:

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

关于android - 充气 View 的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42334008/

相关文章:

android - 在 CheckBoxPreference 中分别点击 Text 和 Checkbox

mysql - 记录未出现在 View 中,但出现在 View 查询结果中

java - 在我的布局中获取两个宽度为 50/50 的按钮

xaml - 即使已明确设置,Windows Phone 列表框项目背景也会改变颜色

java - 什么是 Vaadin UI 以及如何在 UI 之间导航

android - onLongClick 后在特定位置显示图像

android - 编译项目时报错25.0.0

android - 如何在 Android 中为 TextView 设置 LAYOUTGRAVITY?

android - 调整无宽高比的图像大小

android - 在多个膨胀布局中获取对同一 View 的引用