我不知道爲什麼我的兩個按鈕位於左側角落? 他們應該像第三個!我沒有發現任何錯誤。如果有人知道什麼是錯的,那將會很好。謝謝!我的2個按鈕位於左上角,不知道爲什麼?
代碼:
RelativeLayout relativeLayout = new RelativeLayout(this);
TextView tvDate = new TextView(this);
tvDate.setText("Date");
tvDate.setId(R.id.tvDate);
RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
tvDate.setLayoutParams(p1);
p1.setMargins(0,30,0,0);
tvDate.setGravity(Gravity.CENTER_HORIZONTAL);
tvDate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
relativeLayout.addView(tvDate);
Button btNew = new Button(this);
btNew.setId(R.id.btNew);
btNew.setText(R.string.btNew);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.setMargins(0,120,0,0);
lp.addRule(RelativeLayout.BELOW, R.id.tvDate);
btNew.setLayoutParams(lp);
relativeLayout.addView(btNew);
Button btShowTests = new Button(this);
btShowTests.setId(R.id.btShowTests);
btNew.setText(R.string.btShowTests);
RelativeLayout.LayoutParams p2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
p2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
p2.setMargins(0,210,0,0);
p2.addRule(RelativeLayout.BELOW, R.id.btNew);
btNew.setLayoutParams(p2);
relativeLayout.addView(btShowTests);
Button btCheckTest = new Button(this);
btNew.setText(R.string.btShowTests);
btNew.setId(R.id.btCheckTest);
RelativeLayout.LayoutParams p3 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
p3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
p3.setMargins(0,300,0,0);
p3.addRule(RelativeLayout.BELOW, R.id.btShowTests);
btNew.setLayoutParams(p3);
relativeLayout.addView(btCheckTest);
this.setContentView(relativeLayout);
您是否嘗試過不設定邊距? – Gotiasits