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