简体   繁体   中英

onFinishInflate() never gets called

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() 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, ie :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM