自定义View的onDraw 函数不执行

解决办法:

    在自定义的View 的构造方法中添加一句话:   this.setWillNotDraw(false);


解释:

那么加这条语句的作用是什么?先看API:

        If this view doesn't do any drawing on its own, set this flag to allow further optimizations. By default, this flag is not set on View, but could be set on some View subclasses such as ViewGroup. Typically, if you override onDraw(Canvas) you should clear this flag.

        如果在当前的view上面不做任何的绘制操作,需要设置这个标记以便将来的更好的需要,默认的,这个标记在View里是不设定的。但是像View的一些子类如ViewGroup是可以设定的,典型的,你如果复写了onDraw(Canvas)方法,你需要清除此标记。

原文地址:https://www.cnblogs.com/neo-java/p/6830664.html