简体   繁体   English

Android - onDraw未在动态插入的自定义视图中调用

[英]Android - onDraw not being called in dynamically inserted custom view

I have a custom view being dynamically inserted and the onDraw is not being called. 我有一个动态插入的自定义视图,并且没有调用onDraw I initially made the custom view without the dynamic insertion and it worked fine, however when moving it to my working code base, I cannot get onDraw to get called ever, even though the view constructor gets called and the onMeasure also gets called. 我最初在没有动态插入的情况下创建了自定义视图,并且工作正常,但是当将其移动到我的工作代码库时,即使视图构造函数被调用并且onMeasure也被调用,我也无法让onDraw被调用。 I've tried putting invalidate() and postInvalidate() calls around the place, but they don't seem to force the view to draw. 我已经尝试在这个地方放置invalidate()postInvalidate()调用,但它们似乎并没有强制视图绘制。 The view hierarchy looks the same for both version and I'm lost. 对于这两个版本,视图层次结构看起来都相同,而我丢失了。

Can anyone help? 有人可以帮忙吗?

EDIT: A bit more testing has shown that onDraw is getting called on the emulator, but not the handset. 编辑:更多的测试表明, onDraw正在仿真器上调用,而不是手机。 Also onSizeChanged on the handset has the correct values coming through, but on the emulator the values are all 0. 手机上的onSizeChanged也有正确的值,但在仿真器上的值都是0。

EDIT: I've been playing about with it and still can't get anywhere, so have put a bounty on it. 编辑:我一直在玩它仍然无法到达任何地方,所以已经给它一个赏金。 If you need more code / xml / info then please let me know. 如果您需要更多代码/ xml / info,请告诉我们。

This sounds a lot like a problem i had recently, with my first custom view. 这听起来很像我最近的问题,我的第一个自定义视图。 Did you notice if, when loading view hierarchy, your custom views show up? 在加载视图层次结构时,您是否注意到自定义视图是否显示? and anyway do they appear laid out in the wireframe preview or not? 无论如何它们是否在线框预览中显示?

If the latter is the case, it's requestLayout() that doesn't get called (loading view hierarchy calls that on every view - that's why it makes views show up). 如果是后者,则不会调用requestLayout() (加载视图层次结构会在每个视图上调用 - 这就是为什么它会显示视图)。 i've also found that a similar bug was reported already here . 我还发现这里已经报道过类似的bug

我将View更改为TextView ,它似乎现在正常工作,虽然我有点困惑为什么 View不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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