gpt4 book ai didi

android - 我的 2 个按钮在左下角,不知道为什么?

转载 作者:行者123 更新时间:2023-11-30 00:22:13 29 4
gpt4 key购买 nike

我不知道为什么我的两个按钮在左下角?他们应该像第三个一样!我还没有发现任何错误。如果有人知道出了什么问题,那就太好了。谢谢!

在这里你可以看到它的实际外观 enter image description here

代码:

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);

最佳答案

您的代码中有两个问题。第一个是用于“日期”TextViewRelativeLayout.LayoutParams。你有这个:

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。完成所有这些后,您的按钮将全部出现:

enter image description here

(请注意,第三个按钮重复使用与第二个按钮相同的字符串。您可能也想更改它。)

关于android - 我的 2 个按钮在左下角,不知道为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46059096/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com