简体   繁体   English

Android Fragment onCreateView() 方法中的容器是什么

[英]Android Fragment what container is it in onCreateView() method

I wonder what is container parameter in onCreateView() , cause when i inflate view to that container it make me wonder what viewGroup is this,is it a viewGroup from the activity that we will add a fragment to ?我想知道onCreateView()容器参数是什么,因为当我将视图膨胀到该容器时,它让我想知道这是什么 viewGroup,它是我们将添加片段的活动中的 viewGroup 吗? if it is true then why we need to attach it in inflate method cause i think we will add this fragment to the viewgroup of activity in activity's xml anyway.如果是真的,那么为什么我们需要在 inflate 方法中附加它,因为我认为我们无论如何都会将此片段添加到活动的 xml 中的活动视图组中。

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_top,container,true);
    return view;
}

The inflate() method takes three arguments: inflate()方法接受三个参数:

  • The resource ID of the layout you want to inflate.要膨胀的布局的resource ID

  • The ViewGroup to be the parent of the inflated layout.要成为膨胀布局的父级的ViewGroup Passing the container is important in order for the system to apply layout parameters to the root view of the inflated layout, specified by the parent view in which it's going.传递容器对于系统将布局参数应用于膨胀布局的根视图非常重要,该根视图由其所在的父视图指定。

  • A boolean indicating whether the inflated layout should be attached to the ViewGroup (the second parameter) during inflation.一个boolean指示在膨胀期间是否应将膨胀的布局附加到 ViewGroup(第二个参数)。

For more refer here有关更多信息,请参阅 此处

To be more specific, I think the container is the FragmentContainerView in this example .更具体地说,我认为容器是本例中的 FragmentContainerView 。 Basically it's the resource id which you add your fragment to.基本上它是您将片段添加到的资源 ID。 For example, if we do例如,如果我们这样做

fragmentTransaction.add(R.id.container_view, fragment).commitNow();

Then the container is the ViewGroup identified by R.id.container_view.那么容器就是R.id.container_view标识的ViewGroup。

Quoting the docs引用文档

container ViewGroup: If non-null, this is the parent view that the fragment's UI should be attached to.容器视图组:如果非空,这是片段的 UI 应该附加到的父视图。 The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view.该片段不应添加视图本身,但这可用于生成视图的 LayoutParams。 This value may be null.该值可能为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM