回调之后所有子视图从XML已添加到ViewGroup? [英] Callback after all child Views from XML have been added to ViewGroup?
问题描述
我在写一个自定义 ViewGroup
,遇到了以下问题:
I was writing a custom ViewGroup
and came across the following problem:
ViewGroup
应该只能通过在XML中指定属性来使用。
然而,我想在 ViewGroup
创建之后,在代码中做一些内部初始化,并且所有的子 View $ c $添加了来自XML的c> s(即,当布局充气器已添加
ViewGroup $ c $的所有子
View
The ViewGroup
should be usable by specifying properties in XML only.
However, I want to do some internal initialization in code after the ViewGroup
has been created and all its child View
s from XML have been added (that is, when the layout inflator has added all the child View
s of a ViewGroup
specified in XML).
我发现的所有相关的是使用 getViewTreeObserver()的建议addOnGlobalLayoutListener(...) code>。
然而,这至少在每个子查看
之后被调用,并且在恢复应用程序等之后被调用。因此它甚至不能检测到时刻当所有子查看
已添加。
All I found related to this are recommendations to use getViewTreeObserver().addOnGlobalLayoutListener(...)
.
However, this is called at least after each child View
is added and also after resuming an app etc. So it does not even make it possible to detect the moment when all child View
s have been added.
是否有一个方法调用所有子查看已添加到
ViewGroup
?
Is there a method called after all child View
s have been added to a ViewGroup
?
相关: =http://stackoverflow.com/q/17334078/905686>从XML中将子视图添加到Layout / ViewGroup
Related: When are child views added to Layout/ViewGroup from XML
推荐答案
有一个回调: View.onFinishInflate()
。
从文档(其中还有一个实现一个自定义视图描述所有的回调):
There is a callback: View.onFinishInflate()
.
From the documentation (which also has a section "Implementing a Custom View" describing all the callbacks):
从XML完成一个视图。
p>只需覆盖自定义 ViewGroup
中的方法:
Simply override the method in your custom ViewGroup
:
public class MyViewGroup extends ViewGroup {
@Override
protected void onFinishInflate() {
super.onFinishInflate();
doMyInitialization();
}
}
这篇关于回调之后所有子视图从XML已添加到ViewGroup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!