开发者

LayoutInflater and onFinishInflate()

开发者 https://www.devze.com 2023-03-12 03:54 出处:网络
I am running into the findViewById() returning NULL problem that a lot of them seem to encounter. I understand that the findViewById() should only be called after the View is inflated in onFinishInfla

I am running into the findViewById() returning NULL problem that a lot of them seem to encounter. I understand that the findViewById() should only be called after the View is inflated in onFinishInflate(). My question is - what is view inflation?

In the Activity I call setContentView(R.layout.testview) which should call the View constructor and also calls the onDraw(). So that inflates the view does it not? If so why is there a LayoutInflater and a function to actually inflate the view? In fact in my code onInflateView() is never called even though the whole view is rendered and I was able to interact with the program. That tells me that View inflation is somehow different from calling the View's constructor and onDraw() functions. Can someone explain pl开发者_运维知识库ease?

EDIT: Also I have a custom view that I draw using the onDraw() function. Somehow the onFinishInflate() function is never called for me. What can be the reason for this?

-P


My question is - what is view inflation?

View inflation is the act of converting a layout XML file into the corresponding tree of View objects. You manually do this using LayoutInflater, or Android does it for you from setContentView(), built-in Adapter classes, etc.

In the Activity I call setContentView(R.layout.testview) which should call the View constructor and also calls the onDraw(). So that inflates the view does it not?

setContentView() inflates the supplied layout file.

If so why is there a LayoutInflater and a function to actually inflate the view?

Because sometimes you need to do it by hand, such as in a complex ListView with different types of rows (e.g.,. header and detail).

0

精彩评论

暂无评论...
验证码 换一张
取 消