好吧,我这样做(不是实际代码)
try {
final View view = LayoutInflater.from(context).inflate(R.layout.ownComponent, null);
} catch (InflateExpection e) {
}
if (view != null) {
// This child is null about 5/10 times!
View child = view.findViewById(R.id.ownComponentChild);
}
我读到在膨胀之后不能保证 subview 被膨胀,那么当所有 child 都准备好时获得回调的巧妙方法是什么?
最佳答案
也许我误解了你想做的事情,但看起来你正在膨胀一个 View
而不是一个布局......
尝试
View view = LayoutInflater.from(context).inflate(R.layout.LAYOUT_THAT_HOLDS_ownComponent, this, true);
然后 View 将包含整个布局,您可以从中通过 Id 找到 child
view.findViewById(...);
编辑:
很难知道它是否相关,因为您没有发布足够的代码,但试试这个:
从 try/catch 中获取 View view
并将其作为类成员。松开决赛并投下 child 。
示例(假设 ownComponentChild 是一个 FrameLayout):
FrameLayout child = (FrameLayout)view.findViewById(R.id.ownComponentChild);
关于android - 膨胀后 subview 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10514637/