简体   繁体   English

Android - 视图未正确对齐

[英]Android - Views are not aligned correctly

Here is my code : 这是我的代码:

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

and while these views should be centered horizontally and tactical they appear like that : 虽然这些视图应该以水平和战术为中心,但它们看起来像这样:

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

I want each view to be under another view and all views should be horizontally in the center ! 我希望每个视图都在另一个视图下,所有视图都应该在中心水平! RatingBar(rtb) should be in the top and under that should be RatingBar(rtb)应该在顶部,在它之下应该是

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

I have spent to much time quering this issue and i cant find the answer why this is happening thank you for your help :) 我花了很多时间来解决这个问题,我无法找到答案为什么会发生这种情况谢谢你的帮助:)

You need to setId() some identifiers to your views so that the ALIGN_TOP, view.getId() rules actually do something. 你需要setId()一些标识符,以您的意见,使ALIGN_TOP, view.getId()实际上规则做一些事情。

Though ALIGN_TOP only aligns the top of the view, making the views draw on top of each other. 虽然ALIGN_TOP仅对齐视图的顶部,但使视图ALIGN_TOP Use LAYOUT_BELOW rule or a LinearLayout instead. 请改用LAYOUT_BELOW规则或LinearLayout

For a view id, any integer > 0 will do. 对于视图id,任何大于0的整数都可以。 Just make sure the id's are distinct so the RelativeLayout can find the correct views you're referring to. 只需确保id是不同的,所以RelativeLayout可以找到你所指的正确视图。

For Android Studio's "Expected resource of type Id" error, use @IdRes annotation to annotate your integers as resource identifiers. 对于Android Studio的“ID类型的预期资源”错误,请使用@IdRes批注将整数注释为资源标识符。 For example, 例如,

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

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

... though in this case a LinearLayout would avoid the need of generating ids in the first place. ...虽然在这种情况下, LinearLayout可以避免首先生成id的需要。

Since you are using RelativeLayout use txt1_params.addRule(RelativeLayout.BELOW, rtb.getId()); 由于您使用的是RelativeLayout,请使用txt1_params.addRule(RelativeLayout.BELOW,rtb.getId()); This should do the trick.. 这应该做的伎俩..

Sample Code for txt1 below rtb rtb下面的txt1的示例代码

txt1_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
txt1_params.addRule(RelativeLayout.BELOW, rtb.getId());
txt1_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
txt1.setLayoutParams(txt1_params);

You can use the same code for rest of the views. 您可以对其余视图使用相同的代码。

Assign id via code (programmatically) 通过代码分配id(以编程方式)

  • Manually set ids using someView.setId(int); 使用someView.setId(int)手动设置id;
  • The int must be positive, but is otherwise arbitrary- it can be whatever you want (keep reading if this is frightful.) int必须是正数,否则是任意的 - 它可以是你想要的任何东西(如果这是可怕的话,继续阅读。)
  • For example, if creating and numbering several views representing items, you could use their item number. 例如,如果创建和编号表示项目的多个视图,则可以使用其项目编号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM