我有一个“MainActivity.java”类,以及一个内部类“SquashCourtView”,它扩展了SurfaceView
并实现了Runnable
。<登记/>
我正在尝试改变背景颜色,但没有succsess。有什么帮助吗?
// all required things are imported
public class MainActivity extends AppCompatActivity {
Canvas canvas;
SquashCourtView squashCourtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
squashCourtView = new SquashCourtView(this);
setContentView(squashCourtView);
}
class SquashCourtView extends SurfaceView implements Runnable {
Thread ourThread = null;
SurfaceHolder ourHolder;
Paint paint;
public SquashCourtView(Context context) {
super(context);
ourHolder = getHolder();
paint = new Paint();
}
@Override
public void run() {
drawCourt();
}
public void drawCourt() {
if (ourHolder.getSurface().isValid()) {
canvas = ourHolder.lockCanvas();
canvas.drawColor(Color.BLACK);//the background
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
答案 0 :(得分:0)
只打电话super.onCreate(savedInstanceState);
没有什么额外的,只需添加此
squashCourtView.setBackgroundColor(Color.RED);
你的onCreate将是
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
squashCourtView = new SquashCourtView(this);
setContentView(squashCourtView);
squashCourtView.setBackgroundColor(Color.RED);
}
快乐编码