使用eclipse創建我的第一個遊戲程序。我的問題是。有沒有可能設置一個按鈕,而不使用XML?設置不使用XML佈局的按鈕
在聲明2個setcontentviews時遇到問題。該按鈕實際上並未出現在遊戲中。有人能幫助我嗎?
public class Game extends Activity {
Maze maze;
public void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
this.maze = (Maze)getLastNonConfigurationInstance();
if(this.maze == null) {
this.maze = (Maze)extras.get("maze");
}
GameView view = new GameView(this);
view.setMaze(this.maze);
Button myButton = new Button(this);
myButton.setText("Press me");
myButton.setBackgroundColor(Color.YELLOW);
RelativeLayout myLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
myLayout.addView(myButton, buttonParams);
setContentView(myLayout);
setContentView(view);
}
}
http://www.techotopia.com/index.php/Creating_an_Android_User_Interface_in_Java_Code – user184994
感謝@ user184994 – icecream
沒有probs。如果您有任何更多(特定)問題,請編輯此問題。 – user184994