lin*_*fan 6 android android-layout
我试图用代码将视图扩展到容器中,并同时获得对扩展视图(按钮)的引用。所以代码是这样的:
Button mybut = (Button) getLayoutInflater().inflate(resID, lyMain, true);
Run Code Online (Sandbox Code Playgroud)
但这是行不通的:返回的结果不是我期望的。在https://developer.android.com/reference/android/view/LayoutInflater.html上阅读文档,我发现:
(inflate...returns)... The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.
现在是愚蠢的问题:为什么此方法应返回我已经知道的东西(当我传递根和“ true”以附加到它时)?我很有可能需要引用刚刚膨胀的View,而不是引用传递给该方法的根/容器。
小智 6
确实很奇怪。但是执行以下操作将得到完全相同的结果,但实际上会捕获膨胀的布局:
Button mybut = (Button) getLayoutInflater().inflate(resID, lyMain, false);
lyMain.addView(mybut);
Run Code Online (Sandbox Code Playgroud)
所以它确实需要一行额外的代码,但与必须调用findViewById或相比,这对我来说似乎是一个小小的不便getView(index)。
该文档很好地解释了差异,但我想您已经阅读过:
root:作为生成的层次结构的父级的可选视图(如果attachToRoot 为true),或者只是一个为返回的层次结构的根提供一组LayoutParams 值的对象(如果attachToRoot 为false。)
| 归档时间: |
|
| 查看次数: |
849 次 |
| 最近记录: |