在Android片段中膨胀布局

在Android片段中膨胀布局,android,android-fragments,Android,Android Fragments,我在我的代码库中看到了两种在片段中膨胀布局的方法,有什么区别?是否有最佳实践 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return View.inflate(getActivity(), R.layout.mylayout, null); } 或: 请参阅视图的来源。充气(): 因此,在内部,View类的in

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

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


请参阅
视图的来源。充气()

因此,在内部,
View
类的
inflate()
方法使用了
LayoutInflater
,这使我假设没有区别


如果查看第一个方法视图,请充气(getActivity(),R.layout.mylayout,null); 您将看到它代表第二个充气器。充气(R.layout.mylayout,container,false);
因此,它们之间没有区别。

它们都有字面上相同的含义,在运行时也没有区别。

这取决于您希望如何使用,或者哪一个最适合您的需求。这是一个很好的解释。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.mylayout, container, false);
}
public static View inflate(Context context, int resource, ViewGroup root) {
        LayoutInflater factory = LayoutInflater.from(context);
        return factory.inflate(resource, root);
}