如果我從代碼創建佈局如何解決解決方案的問題?解決問題與佈局
我在320 x 480分辨率的手機上測試過它,看起來沒問題。但是當我在我的朋友手機(Galaxy S)測試它時,看起來佈局沒有伸展。
爲什麼我從代碼創建佈局是因爲我不知道如何將按鈕放置在特定位置。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rLayout = new RelativeLayout(this);//(RelativeLayout)findViewById(R.id.relativeLayout1);
rLayout.setBackgroundResource(R.drawable.menu_blankscreen);
InitializeLayout();
setContentView(rLayout);
}
private void InitializeLayout() {
ImageButton newGame = new ImageButton(this);
//newGame.setBackgroundResource(drawable.menu_button_new);
newGame.setBackgroundResource(R.drawable.menu_button_new_xml);
newGame.setOnClickListener(newGameClick);
addView(newGame, 5, 174, 149, 41);
ImageView continueGame = new ImageView(this);
continueGame.setBackgroundResource(R.drawable.menu_button_continue_xml);
continueGame.setOnClickListener(continueClick);
addView(continueGame, 5, 218, 149, 41);
ImageView highscore = new ImageView(this);
highscore.setBackgroundResource(R.drawable.menu_button_highscore_xml);
highscore.setOnClickListener(highscoreClick);
addView(highscore, 5, 262, 149, 41);
ImageView achievement = new ImageView(this);
achievement.setBackgroundResource(R.drawable.menu_button_achievement_xml);
achievement.setOnClickListener(achievementClick);
addView(achievement, 5, 306, 149, 41);
ImageView option = new ImageView(this);
option.setBackgroundResource(R.drawable.menu_button_option_xml);
option.setOnClickListener(optionClick);
addView(option, 5, 350, 149, 41);
ImageView quit = new ImageView(this);
quit.setBackgroundResource(R.drawable.menu_button_quit_xml);
quit.setOnClickListener(quitClick);
addView(quit, 5, 395, 149, 41);
Button btnTest = new Button(this);
btnTest.setText("Test");
btnTest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(StartMenuActivity.this, TestActivity.class);
startActivity(intent);
}
});
addView(btnTest, 270, 430,50, 50);
}
public void addView(View view, int x, int y, int width, int height) {
rParam = new RelativeLayout.LayoutParams(width, height);
rParam.leftMargin = x;
rParam.topMargin = y;
rLayout.addView(view, rParam);
}
我假設leftMargin爲x,topMargin爲y。 它是否與像素有關?
我真的不明白..這是否意味着只是將按鈕放在我想要的位置將需要很大的努力? – Fugogugo 2011-04-01 05:21:28
這意味着,雖然自定義視圖是可能的,甚至在某些情況下是可取的...如果你不知道你在做什麼,應該避免它們。需要努力去理解你想要做的事情。 – Martinez 2011-04-01 05:34:23