首页 > 解决方案 > Android Fragment 中的膨胀布局

问题描述

在我的代码库中,我看到了两种在片段中扩展布局的方法,有什么区别?有最佳实践吗?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return View.inflate(getActivity(), R.layout.mylayout, null);
}

或者 :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.mylayout, container, false);
}

标签: androidandroid-fragments

解决方案


见来源View.inflate()

public static View inflate(Context context, int resource, ViewGroup root) {
        LayoutInflater factory = LayoutInflater.from(context);
        return factory.inflate(resource, root);
}

因此,在内部,class的inflate()方法使用,这让我假设没有区别。ViewLayoutInflater

参考


推荐阅读