膨胀布局方法有什么区别,这些参数有什么用
what is is the difference between inflating layout methods and what are these parameters for
我发现您可以通过这 3 种方式扩充布局。但是无法在各自的语句中使用第二个和第三个参数。 ViewGroup 和 attachedToroot 参数有什么用?
首次用于外部 Activity:
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate( R.layout.myNewInflatedLayout, ViewGroup);
View view = inflater.inflate( R.layout.myNewInflatedLayout, ViewGroup, attachedToroot);
Button myButton = (Button) view.findViewById( R.id.myButton );
如果您查看 developer.android.com 作为参考,您将看到四种 public 方法。
最常用的两个是你在问题中写的。
inflater.inflate( R.layout.myNewInflatedLayout, ViewGroup);
inflater.inflate( R.layout.myNewInflatedLayout, ViewGroup,
attachedToroot);
这里摘录了非常棒的 article 关于布局膨胀的内容。
The first parameter points to the layout resource you want to inflate.
The second parameter is the root view of the hierarchy you are
inflating the resource to attach to. When the third parameter is
present, it governs whether or not the inflated view is attached to
the supplied root after inflation.
我建议你读到最后。希望这有帮助。 :)
我会尽力解答你对第二个参数和第三个参数的使用的疑惑。
第二个参数是 ViewGroup,在文档中描述为:
public View inflate (int resource, ViewGroup root)
Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.
root Optional view to be the parent of the generated hierarchy.
Returns
The root View of the inflated hierarchy. If root was supplied, this is
the root View; otherwise it is the root of the inflated XML file.
基于此,我看到了 2 种可能性:
您将视图组设置为空。在这种情况下,您的布局将在 activity 中膨胀,但该膨胀层次结构的根视图将是 xml 文件的根。因此,如果您的 xml 具有相对布局或任何其他布局作为根元素,那将成为膨胀层次结构的根。你会有这样的东西:
<xml> // Whatever is the root of this xml, is your root for the inflated hierarchy.
如果您将视图组设置为其他内容,它将成为膨胀层次结构的根。所以,实际上你会有这样的事情:
<Your Viewgroup>
<Your xml>
</Your Viewgroup>
在这里很明显,在这种情况下,您指定的视图组将是膨胀层次结构的父级。所以我认为这完全取决于您的要求。
关于第三个参数:Confusion regarding inflater.inflate Android documentation
您可以阅读我对此 link 的回答,以了解更多信息。希望对您有所帮助。
我发现您可以通过这 3 种方式扩充布局。但是无法在各自的语句中使用第二个和第三个参数。 ViewGroup 和 attachedToroot 参数有什么用?
首次用于外部 Activity:
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate( R.layout.myNewInflatedLayout, ViewGroup);
View view = inflater.inflate( R.layout.myNewInflatedLayout, ViewGroup, attachedToroot);
Button myButton = (Button) view.findViewById( R.id.myButton );
如果您查看 developer.android.com 作为参考,您将看到四种 public 方法。
最常用的两个是你在问题中写的。
inflater.inflate( R.layout.myNewInflatedLayout, ViewGroup);
inflater.inflate( R.layout.myNewInflatedLayout, ViewGroup,
attachedToroot);
这里摘录了非常棒的 article 关于布局膨胀的内容。
The first parameter points to the layout resource you want to inflate. The second parameter is the root view of the hierarchy you are inflating the resource to attach to. When the third parameter is present, it governs whether or not the inflated view is attached to the supplied root after inflation.
我建议你读到最后。希望这有帮助。 :)
我会尽力解答你对第二个参数和第三个参数的使用的疑惑。
第二个参数是 ViewGroup,在文档中描述为:
public View inflate (int resource, ViewGroup root)
Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.
root Optional view to be the parent of the generated hierarchy.
Returns
The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.
基于此,我看到了 2 种可能性:
您将视图组设置为空。在这种情况下,您的布局将在 activity 中膨胀,但该膨胀层次结构的根视图将是 xml 文件的根。因此,如果您的 xml 具有相对布局或任何其他布局作为根元素,那将成为膨胀层次结构的根。你会有这样的东西:
<xml> // Whatever is the root of this xml, is your root for the inflated hierarchy.
如果您将视图组设置为其他内容,它将成为膨胀层次结构的根。所以,实际上你会有这样的事情:
<Your Viewgroup> <Your xml> </Your Viewgroup>
在这里很明显,在这种情况下,您指定的视图组将是膨胀层次结构的父级。所以我认为这完全取决于您的要求。
关于第三个参数:Confusion regarding inflater.inflate Android documentation
您可以阅读我对此 link 的回答,以了解更多信息。希望对您有所帮助。