onFinishInflate() never gets called

点点圈 提交于 2020-01-31 06:16:08

问题


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?


回答1:


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.

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" />
...

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(...).

Alternatively, if you created an instance of CustomView with:

...
new CustomView(context);
...

...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.



来源:https://stackoverflow.com/questions/9650906/onfinishinflate-never-gets-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!