我正在通过扩展创建一个自定义小部件LinearLayout
:
public class MyWidget extends LinearLayout {
private static Paint PAINT = new Paint(Paint.ANTI_ALIAS_FLAG);
static {
PAINT.setColor(Color.RED);
}
public MyWidget(Context context) {
this(context, null);
}
public MyWidget(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight()/2, canvas.getWidth()/2, PAINT);
// never gets called :-(
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
// this gets called, but with a canvas sized after the padding.
}
}
我可以很好地添加孩子,但我永远不会onDraw()
调用我的自定义。dispatchDraw()
被调用,但这似乎有一个不同的画布(填充内的画布。我需要在整个布局区域上绘制)。是否需要设置一些标志才能onDraw()
调用布局?