gpt4 book ai didi

Android - View 未正确对齐

转载 作者:行者123 更新时间:2023-11-29 17:47:54 25 4
gpt4 key购买 nike

这是我的代码:

  rl=(RelativeLayout)findViewById(R.id.ScoreLayout);


rtb=new RatingBar(this);
rl.addView(rtb);
rtb.setNumStars(5);
rtb_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
rtb_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
rtb_params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rtb.setLayoutParams(rtb_params);


txt1=new TextView(this);
rl.addView(txt1);
txt1.setSingleLine();
txt1.setTextSize(TypedValue.COMPLEX_UNIT_PX,15);
txt1.setText("Best Score: ");
txt1_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
txt1_params.addRule(RelativeLayout.ALIGN_TOP, rtb.getId());
txt1_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
txt1.setLayoutParams(txt1_params);


txt2=new TextView(this);
rl.addView(txt2);
txt2.setSingleLine();
txt2.setTextSize(TypedValue.COMPLEX_UNIT_PX,30);
txt2.setText(String.valueOf(DataBase.HighScore));
txt2_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
txt2_params.addRule(RelativeLayout.ALIGN_TOP, txt1.getId());
txt2_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
txt2.setLayoutParams(txt1_params);


txt3=new TextView(this);
rl.addView(txt3);
txt3.setSingleLine();
txt3.setTextSize(TypedValue.COMPLEX_UNIT_PX,15);
txt2.setText("Current Score: ");
txt3_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
txt3_params.addRule(RelativeLayout.ALIGN_TOP, txt2.getId());
txt3_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
txt3.setLayoutParams(txt1_params);


txt4=new TextView(this);
rl.addView(txt4);
txt4.setSingleLine();
txt4.setTextSize(TypedValue.COMPLEX_UNIT_PX,30);
txt4.setText(String.valueOf(DataBase.score));
txt4_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
txt4_params.addRule(RelativeLayout.ALIGN_TOP, txt3.getId());
txt4_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
txt4.setLayoutParams(txt1_params);


btn1=new Button(this);
rl.addView(btn1);
btn1_params=new RelativeLayout.LayoutParams(150,40);
btn1_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
btn1_params.topMargin=DataBase.Layout_Height-80-80;//first 80 for the advertisment Bar and second for the two 40-height-buttons
btn1.setLayoutParams(btn1_params);


btn2=new Button(this);
rl.addView(btn2);
btn2_params=new RelativeLayout.LayoutParams(150,40);
btn2_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
btn2_params.addRule(RelativeLayout.ALIGN_TOP, btn1.getId());
btn2.setLayoutParams(btn2_params);

虽然这些 View 应该水平居中并且很有策略,但它们看起来像这样:

http://postimg.org/image/peqhzw0uh/

我希望每个 View 都在另一个 View 之下,并且所有 View 都应该水平居中! RatingBar(rtb) 应该在上面,下面应该是

txt1-->txt2-->txt3-->txt4-->btn1-->btn2

我花了很多时间来查询这个问题,但我找不到为什么会这样的答案谢谢你的帮助:)

最佳答案

您需要为您的 View setId() 一些标识符,以便ALIGN_TOP, view.getId() 规则实际执行某些操作。

尽管 ALIGN_TOP 只对齐 View 的顶部,使 View 在彼此之上绘制。请改用 LAYOUT_BELOW 规则或 LinearLayout

对于 View ID,任何大于 0 的整数都可以。只需确保 ID 不同,以便 RelativeLayout 可以找到您所指的正确 View 。

对于 Android Studio 的“Expected resource of type Id”错误,使用 @IdRes 注释将您的整数注释为资源标识符。例如,

@android.support.annotation.IdRes int id = 1;
view.setId(id); // no error

id++; // next id
view2.setId(id); // and so on

...虽然在这种情况下,LinearLayout 将首先避免生成 id 的需要。

关于Android - View 未正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25137642/

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