He encontrado muchas publicaciones en stackoverflow, pero todavía no puedo resolver mi problema. Aquí está mi pieza de código:
public class MyView extends RelativeLayout {
Button b1;
Button b2;
Context sContext;
public static int i = 0;
private int w = 400;
private int h = 400;
private int w2 = 100;
private int h2 = 100;
public MyView(Context context) {
super(context);
sContext = context;
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
sContext = context;
init();
}
private void init() {
b1 = new Button(sContext);
addView(b1);
b1.setBackgroundColor(Color.YELLOW);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (w >= 600) {
MyView.this.setBackgroundColor(Color.GREEN);
//b1.setBackgroundColor(Color.RED);
} else {
MyView.this.setX(100);
}
MyView.this.invalidate();
w += 100;
w2 += 20;
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//b1.setBackgroundColor(Color.RED);
Toast.makeText(sContext, ""+i, Toast.LENGTH_SHORT).show();
++i;
}
}
¿Podría explicar por qué onDraw no se llama las tres primeras veces que presiono b1? Porque llamé invalidar cada vez que presiono b1. ¡Muchas gracias!
Respuestas:5 Respuestas 5
Tiempo:hace 9 años, 1 mes
Última modificación:hace 1 año, 8 meses
Solución
De forma predeterminada, todas las subclases de ViewGroup no llaman a su método, debe habilitarlo llamando al vínculoonDraw
setWillNotDraw(false)
Otras respuestas
En mi caso no me llamaron porque volví como ancho o alto de vista.onDraw
0