为什么OnDraw中没有无效后,叫()? [英] Why onDraw is not called after invalidate()?
本文介绍了为什么OnDraw中没有无效后,叫()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现计算器上很多帖子,但我还是解决不了我的问题。这是我的code片:
I have found many posts on stackoverflow but I still cannot solve my problem. Here is my code piece:
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;
}
}
请您解释一下为什么OnDraw的不叫前三次我preSS B1?因为我叫无效,每次我preSS B1。非常感谢你!
Would you please explain why onDraw is not called the first three times I press b1? Because I called invalidate everytime I press b1. Thank you very much!
推荐答案
在默认情况下所有的ViewGroup子类没有调用它的OnDraw方法,你应该调用setWillNotDraw启用它(假)<一href="http://developer.android.com/reference/android/view/View.html#setWillNotDraw%28boolean%29">link
By default all ViewGroup sub classes did not calls its onDraw method, you should enable it by call setWillNotDraw (false) link
这篇关于为什么OnDraw中没有无效后,叫()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文