2016-06-18 297 views
-1

我有一個「MainActivity.java」類,並且延伸SurfaceView並實現Runnable一個內部類「SquashCourtView」
我想改變背景顏色,但沒有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

呼叫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); 
} 

快樂編碼