Android LayoutFlater attachToRoot参数是什么意思?

Android LayoutFlater attachToRoot参数是什么意思?,android,android-layout,android-view,layout-inflater,Android,Android Layout,Android View,Layout Inflater,文档中没有明确说明attachToRoot参数的用途 attachToRoot:膨胀的层次结构是否应附加到根参数?如果为false,则root仅用于创建正确的 XML中根视图的LayoutParams的子类 有人能更详细地解释一下根视图是什么,并举例说明true和false值之间的行为变化吗?当您定义父级时,attachToRoot确定您是否希望充气机实际连接到父级。在某些情况下,这会导致问题,例如在ListAdapter中,它会导致异常,因为列表试图将视图添加到列表中,但它表示视图已附加。在另

文档中没有明确说明
attachToRoot
参数的用途

attachToRoot:膨胀的层次结构是否应附加到根参数?如果为false,则root仅用于创建正确的 XML中根视图的LayoutParams的子类


有人能更详细地解释一下根视图是什么,并举例说明
true
false
值之间的行为变化吗?

当您定义父级时,attachToRoot确定您是否希望充气机实际连接到父级。在某些情况下,这会导致问题,例如在ListAdapter中,它会导致异常,因为列表试图将视图添加到列表中,但它表示视图已附加。在另一种情况下,如果您只是自己膨胀视图以添加到活动中,则可以方便地为您保存一行代码。

如果设置为true,则当您的布局膨胀时,它将自动添加到第二个参数中指定的视图组的视图层次结构中作为子级。例如,如果根参数是
LinearLayout
,则膨胀视图将自动添加为该视图的子视图


如果设置为false,则您的版面将被放大,但不会附加到任何其他版面(因此不会绘制、接收触摸事件等)。

文档和前面的两个答案应该足够了,只是我的一些想法

充气
方法用于充气布局文件。对于这些膨胀的布局,您必须能够将它们直接附加到父视图组,或者仅从该布局文件膨胀视图层次,并在正常视图层次之外使用它

在第一种情况下,
attachToRoot
参数必须设置为
true
(或者非常简单地使用
inflate
方法,该方法采用布局文件和父根视图组
(非
null
)。在这种情况下,返回的
视图
只是在方法中传递的
视图组
,即将要添加膨胀视图层次结构的
视图组

对于第二个选项,返回的
视图
是布局文件中的根
视图组
。如果您还记得上一次的讨论,这是
合并
限制的原因之一(当根目录为
合并
的布局文件膨胀时,您必须提供一个父目录,并且
attachedToRoot
必须设置为
true
)。如果布局文件的根为
merge
标记,并且
attachedToRoot
设置为
false
,则
充气
方法将不会返回任何内容,因为
merge
没有等效项。 此外,如文档所述,将
充气
版本的
attachToRoot
设置为
false
非常重要,因为您可以从父级创建具有正确
布局参数的视图层次结构。这在某些情况下很重要,尤其是
AdapterView
的子类
ViewGroup
,不支持
addView()
方法集。我相信您还记得在
getView()方法中使用过这一行:

convertView = inflater.inflate(R.layout.row_layout, parent, false);

此行确保膨胀的
R.layout.row_layout
文件具有根
ViewGroup
上设置的
AdapterView
子类中正确的
LayoutParams
。如果不这样做,则如果根目录是
RelativeLayout
,则布局文件可能会出现一些问题。
TableLayout/TableRow
也有一些特殊而重要的
LayoutParams
,您应该确保其中的视图具有正确的
LayoutParams
,在回答中似乎有很多文本,但没有代码,这就是为什么我决定用一个代码示例重新提出这个老问题,在一些答复中,人们提到:


如果设置为true,则当布局膨胀时,它将自动添加到第2个参数中指定的视图组的视图层次中,作为子视图

这在代码中的实际含义(大多数程序员都理解)是:

注意,前面的代码添加了布局
R.layout.child\u view
作为
MyCustomLayout
的子级,因为
attachToRoot
param是
true
,并以与我将以编程方式使用
addView
完全相同的方式分配父级布局参数,或者就像我在xml中这样做一样:

在前面的代码中,您指定希望
myView
成为它自己的根对象,并且不将其附加到任何父对象,之后我们将其添加为
LinearLayout
的一部分,但有一段时间它是一个独立的(没有父对象)视图

片段也会发生同样的情况,您可以将它们添加到已经存在的组中并成为该组的一部分,或者只传递参数:

充气器。充气(R.layout.fragment,null,false)


指定它将是它自己的根。

我自己也对
充气方法中的
attachToRoot
的真正目的感到困惑。经过一点UI研究,我终于得到了答案:

家长:

inflater.inflate(child,parent,true);
在本例中,是要使用findViewById()充气的视图对象周围的小部件/布局

附件:

inflater.inflate(child,parent,true);
将视图附加到其父视图(将其包含在父层次结构中),以便视图接收到的任何触摸事件也将传输到父视图。现在由家长决定是要娱乐这些活动还是忽略它们。如果设置为false,则不会将它们添加为家长的直接子项,家长也不会收到来自t的任何触摸事件
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setLayoutParams(new LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
    // Create a stand-alone view
View myView = LayoutInflater.from(context)
    .inflate(R.layout.ownRootView, null, false);
linearLayout.addView(myView);
[parent addSubview:inflatedView];
RelativeLayout
           ------->LinearLayout
//here container is the LinearLayout

    View v = Inflater.Inflate(R.layout.image_view_layout,container,true);
//here container is the LinearLayout
    View v = Inflater.Inflate(R.layout.image_view_layout,container,false);
<Button xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/custom_button">
</Button>
inflater.inflate(R.layout.custom_button, mLinearLayout, true);
Button button = (Button) inflater.inflate(R.layout.custom_button,    mLinearLayout, false);
mLinearLayout.addView(button);
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment =  fragmentManager.findFragmentById(R.id.root_viewGroup);

if (fragment == null) {
fragment = new MainFragment();
fragmentManager.beginTransaction()
    .add(R.id.root_viewGroup, fragment)
    .commit();
}
public View onCreateView(LayoutInflater inflater, ViewGroup  parentViewGroup, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout,     parentViewGroup, false);
…
return view;
}
inflater.inflate(child,parent,false);
parent.addView(child);   
inflater.inflate(child,parent,true);
public View onCreateView(LayoutInflater inflater,ViewGroup parent,Bundle bundle)
  {
        super.onCreateView(inflater,parent,bundle);
        View view = inflater.inflate(R.layout.image_fragment,parent,false);
        .....
        return view;
  }
getSupportFragmentManager()
      .beginTransaction()
      .add(parent, childFragment)
      .commit();
View inflate (int resource, ViewGroup root, boolean attachToRoot)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

</LinearLayout>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LayoutInflater inflater = getLayoutInflater();
    LinearLayout root = (LinearLayout) findViewById(R.id.root);
    View view = inflater.inflate(R.layout.button, root, false);
}
root.addView(view);
View view = inflater.inflate(R.layout.button, root, true);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
    .add(R.id.root, fragment)
    .commit();
View view = inflater.inflate(R.layout.button, root, true);
root.addView(view);
View inflate (int resource, ViewGroup root)