使用false attachtoroot和true attachtoroot膨胀布局有什么区别(布尔值)

M.A*_*han 2 android view android-layout

使用false attachtoroot和true attachtoroot(boolean)膨胀布局有什么区别?

这是一个代码:

ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_screen_3, container, false);
Run Code Online (Sandbox Code Playgroud)

和:

ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_screen_3, container, true);
Run Code Online (Sandbox Code Playgroud)

Tan*_*.7x 5

rootattachToRoot参数一起工作.

如果您告诉inflate()要将膨胀的视图附加到根视图,那么您膨胀的布局将作为子项添加到根目录.

以下是该inflate()方法的简化:

public View inflate (int resource, ViewGroup root, boolean attachToRoot) {
    View inflatedView = inflate(resource); // Inflate the desired view

    if (attachToRoot) {
        root.addView(inflatedView);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果要膨胀最终将附加到父视图的视图,这很有用,例如,如果要使用相同的布局对多个视图进行充气以动态填充ListView.