我正在做一个小游戏。但是我希望我activity
拥有 XML 的内容视图,但也希望它显示我稍后创建的视图。
这是我的代码:
public class GameView extends Activity{
MediaPlayer backgroundMusic;
TextView mTextField;
int Px = 0;
int Py = 0;
Point size = new Point();
Display display;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(new myView(this));
setContentView(R.layout.gameviewlayout);
mTextField = (TextView)findViewById(R.id.timer1);
new CountDownTimer(45000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("Seconds Remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
display = getWindowManager().getDefaultDisplay();
display.getSize(size);
int width = size.x;
int height = size.y;
backgroundMusic = MediaPlayer.create(GameView.this, R.raw.gallery_music);
backgroundMusic.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
backgroundMusic.release();
}
private class myView extends View{
public myView(Context context) {
super(context);
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.lil_peanut);
canvas.drawBitmap(Bitmap.createScaledBitmap(bm, 83, 131, false), Px, Py, null);
}
}
}
所以我想要两者setContentView(new myView(this));
,SetContentView(R.layout.gameviewlayout);
我该如何做到这一点?