背景颜色未更改

时间:2016-06-18 05:47:40

标签: java android

我有一个“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);
            }
        }


}

1 个答案:

答案 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);
}

快乐编码