onFinishInflate()不会被调用 [英] onFinishInflate() never gets called
问题描述
谁/什么叫onFinishInflate()?不管我怎么膨胀(在code)我的布局文件,这种方法似乎从来没有被触发。谁能给我一个例子或告诉我,当onFinishInflate()实际上被调用?
Who/What calls onFinishInflate()? No matter how I inflate my layout files (in code) this method never seems to get triggered. Can anyone give me an example or tell me when onFinishInflate() actually get called?
推荐答案
View.onFinishInflate()
被调用后视图(及其子)从XML膨胀。具体来说,就是调用 LayoutInflater.inflate(...)在
的 onFinishInflate()
将被调用。通胀递归执行,从根开始。视图包含儿童可能需要知道什么时候它的孩子们正在完成充气。一个回调的主要用途之一是 ViewGroups
来执行专项行动,一旦它的孩子们准备好了。
View.onFinishInflate()
is called after a view (and its children) is inflated from XML. Specifically, it is during a call to LayoutInflater.inflate(...)
that onFinishInflate()
will be called. Inflation is performed recursively, starting from the root. A view containing children may need to know when its children have finished being inflated. One of the main uses of this callback is for ViewGroups
to perform special actions once its children are ready.
让我们假设你有对查看
名为 CustomView
的子类,它并没有在内部膨胀的任何布局本身。如果你有一个 CustomView
放在你的布局,即:
Let's assume you had a subclass of View
called CustomView
, and that it does not internally inflate any layouts itself. If you had a CustomView
somewhere in your layout, i.e. :
...
<com.company.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
...
您应该看到一个回调 onFinishInflate()
一旦被夸大。如果这是您的活动的主布局,那么你可以考虑在 Activity.setContentView(INT)这之中
被调用。在内部,这将调用 LayoutInflater.inflate(...)
。
You should see a callback to onFinishInflate()
once it has been inflated. If this is in your main layout of your activity, then you can consider this being after Activity.setContentView(int)
is called. Internally, this will call LayoutInflater.inflate(...)
.
另外,如果你创建了 CustomView
的实例有:
Alternatively, if you created an instance of CustomView
with:
...
new CustomView(context);
...
...你不会得到调用 onFinishInflate()
。实例化这样自然会意味着它有没有孩子,因此它不必等待它们被实例中递归方式,如在XML通货膨胀
...you will not get a call to onFinishInflate()
. Instantiating it in this way will naturally mean it has no children and hence it does not have to wait for them to be instantiated in the recursive fashion, as in XML inflation.
这篇关于onFinishInflate()不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!