我不知道为什么我的两个按钮在左下角? 他们应该像第三个!我没有发现任何错误。如果有人知道什么是错的,那就太好了。谢谢!
代码:
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);
答案 0 :(得分:1)
您的代码中存在两个问题。第一个是RelativeLayout.LayoutParams
您用于"日期" TextView
。你有这个:
RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
但您应该使用WRAP_CONTENT
代替身高。如果您将其保留为FILL_PARENT
,则在解决第二个问题后,您将无法看到任何按钮。这是因为TextView
将填满整个屏幕,因此任何BELOW
都会被推出屏幕。
第二个问题是,您的代码错误地引用了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);
在此代码中,当您应该致电btNew.setText()
和btNew.setLayoutParams()
时,您需要致电btShowTests.setText()
和btShowTests.setLayoutParams()
。您的第三个按钮遇到同样的问题:
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);
本节中btNew
的所有引用都应替换为btCheckTest
。完成所有操作后,您的按钮将全部显示:
(请注意,第三个按钮重复使用与第二个按钮相同的字符串。您可能也想要更改它。)