1. 在派生类中,重写onDraw(...)方法,若是不给LinearLayout设置一个背景,系统是不会调用onDraw时,也就是说,咱们重写的onDraw(...)是不会调用的。当设置一个背景后,onDraw就会被调用。ViewGroup自己是一个容器,其自己并无任何东西能够绘制,它是一个透明的控件,因此,不给调用onDraw(...)方法。html
1 /** 2 * If this view doesn't do any drawing on its own, set this flag to 3 * allow further optimizations. By default, this flag is not set on 4 * View, but could be set on some View subclasses such as ViewGroup. 5 * 6 * Typically, if you override {@link #onDraw(android.graphics.Canvas)} 7 * you should clear this flag. 8 * 9 * @param willNotDraw whether or not this View draw on its own 10 */ 11 public void setWillNotDraw(boolean willNotDraw) { 12 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK); 13 }
上面的解释能够看出,想重写ViewGroup.onDraw(...)方法,应该调用这个方法将flag清除。在构造方法中,调用setWillNotDraw(...)方法。android
3、参考资料ide
1. 为何ViewGroup onDraw方法不被调用?this