Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果视图组';s的dispatchDraw方法在android中被覆盖_Android_Android Custom View - Fatal编程技术网

如果视图组';s的dispatchDraw方法在android中被覆盖

如果视图组';s的dispatchDraw方法在android中被覆盖,android,android-custom-view,Android,Android Custom View,我已经编写了一个自定义视图来动态显示圆内的图像 我已覆盖Viewgroup的dispatchDraw方法以绘制圆。在此之后,子ImageView不会显示在屏幕上,如果我不重写该方法,则它们会显示在屏幕上 这是我的班级: public class CustomView extends RelativeLayout { private Paint paint; private View mView; private Context context; private void init(Cont

我已经编写了一个自定义视图来动态显示圆内的图像

我已覆盖Viewgroup的dispatchDraw方法以绘制圆。在此之后,子ImageView不会显示在屏幕上,如果我不重写该方法,则它们会显示在屏幕上

这是我的班级:

public class CustomView extends RelativeLayout {

private Paint paint;
private View mView;
private Context context;


private void init(Context context) {
    LinearLayout layout = new LinearLayout(context);
    layout.setGravity(Gravity.CENTER);
    layout.setOrientation(LinearLayout.VERTICAL);

    // Set generic layout parameters
    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    Button button = new Button(context);
    button.setText("Button!");
    layout.addView(button, params); // Modify this

    ImageView imageView = new ImageView(context);
    imageView.setImageResource(R.drawable.coffe_selected);
    layout.addView(imageView);      

    this.addView(layout);

}

public CustomView(Context mContext) {
    super(mContext);
    context = mContext;

    // create the Paint and set its color
    paint = new Paint();
    paint.setColor(0xFF1f5b83);

    init(context);

}


@Override
protected void dispatchDraw(Canvas canvas) {
    int width = this.getWidth();
    int height = this.getHeight();
    canvas.drawCircle(width / 2, height / 2-64, 200, paint);
}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}
}

查看的源代码以及在
dispatchDraw
中发生的情况

只有一行:

more |= drawChild(canvas, transientChild, drawingTime);
正如你所看到的,孩子们被画在那里。 因此,如果您不调用超级方法
dispatchDraw
,则可能未绘制子对象

只需呼叫:

super.dispatchDraw(canvas);