我遇到了很多人似乎遇到的 findViewById()
返回 NULL 的问题。我知道 findViewById() 应该只在 View 在 onFinishInflate()
中膨胀后调用。我的问题是——什么是观点通胀?
在 Activity 中,我调用了 setContentView(R.layout.testview)
,它应该调用 View 构造函数并调用 onDraw()。所以这会夸大观点,不是吗?如果是这样,为什么会有一个 LayoutInflater
和一个实际膨胀 View 的函数?事实上,在我的代码中 onInflateView()
从未被调用,即使整个 View 被渲染并且我能够与程序交互。这告诉我 View 膨胀与调用 View 的构造函数和 onDraw()
函数有些不同。有人可以解释一下吗?
编辑:我还有一个使用 onDraw() 函数绘制的自定义 View 。不知何故,从来没有为我调用过 onFinishInflate() 函数。这可能是什么原因?
-P
最佳答案
My question is - what is view inflation?
View inflation 是将布局 XML 文件转换为相应的 View
对象树的行为。您可以使用 LayoutInflater
手动执行此操作,或者 Android 通过 setContentView()
、内置 Adapter
类等为您执行此操作。
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()
扩充提供的布局文件。
If so why is there a LayoutInflater and a function to actually inflate the view?
因为有时您需要手动完成,例如在具有不同类型行(例如标题和详细信息)的复杂 ListView
中。
关于android - LayoutInflater 和 onFinishInflate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6304939/