Android ViewGroup
一、概述
二、注意事项
1. 在派生类中,重写onDraw(...)方法,如果不给LinearLayout设置一个背景,系统是不会调用onDraw时,也就是说,我们重写的onDraw(...)是不会调用的。当设置一个背景后,onDraw就会被调用。ViewGroup本身是一个容器,其本身并没有任何东西可以绘制,它是一个透明的控件,所以,不给调用onDraw(...)方法。
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(...)方法。
三、参考资料