RelativeLayout add rule “RelativeLayout.LEFT_OF” not working

前端 未结 6 1218
小蘑菇
小蘑菇 2020-12-17 10:50

I have a relativeLayout like below:



        
相关标签:
6条回答
  • 2020-12-17 11:17

    If you are using a custom id and not a regular generated Android id (eg. R.id.my_id), make sure that the id is not equal to 0 (or negative), otherwise the rule will be ignored.

    0 讨论(0)
  • 2020-12-17 11:21

    I think you might have forgotten to add m_listView to the RelativeLayout or m_listView's visibility would be GONE.

    Can you please check for that?

    0 讨论(0)
  • 2020-12-17 11:24

    setId before align is called, especially for the new object view.

    0 讨论(0)
  • 2020-12-17 11:25

    I think If i put an control A in the relativelayout, then i add control B and declare it's on the left of the control A, the result should be the control B will push the control A to its right, right?

    Your assumption is incorrect, the control A will not be pushed to the right unless you specified this with a RelativeLayout.LayoutParams rule. RelativeLayout places its children one one top of each other starting at the top-left corner of the screen if you don't specify placement rules for them. When you add the View A to the RelativeLayout without any rules(like layout_alignParentRight) it will be placed starting from the top-left corner of the screen. Then, when you add the View B, the rule to_leftOf will apply to this View position but this rule doesn't mean anything for the View A who will maintain its position on the screen. This will make View B to be place to the left of View A but outside of the screen as View A bounds start from the left border of the screen.

    The Button will be placed to the left of the ListView when you use layout_alignParentRight="true" because there is now space to actually see the Button(it's not outside anymore). addView(View child, int index, LayoutParams params) works in a LinearLayout because the LinearLayout arranges its children in a row or column(depending on orientation) so when you add a View at a specific position, it will push the other Views after it to the right or below(depending on orientation)(there is no relative positioning of the views in a LinearLayout, the only rule is that the children come one after the other).

    Starting with the ListView without any rules set on it, here is an example on how to make the Button to appear on the left of the ListView:

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    Button button2 = new Button(this);
    button2.setText("I am button 2");
    button2.setId(1000);
    m_relativeLayout.addView(button2, layoutParams);
    RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) m_listView
                .getLayoutParams();
    rlp.addRule(RelativeLayout.RIGHT_OF, button2.getId());
    

    The Button will be added as normal to the screen and it will appear starting from the top-left corner of the screen. Without the two lines from the code above the Button and ListView will overlap as this is the normal behavior of RelativeLayout for children without any rules on them. We then explicitly modify the position of the ListView to move it to the right(with the last two line from the code above).

    0 讨论(0)
  • 2020-12-17 11:30

    If your variable names are indicative, it's because you are adding the widget to a LinearLayout, so tags for a RelativeLayout get ignored.

    This line is the one I'm talking about:

    m_linearLayout.addView(button2, layoutParams);
    

    EDIT

    You say alignParentRight works... the only difference there is that ot doesn't take an anchor parameter. Perhaps m_listView.getId() isn't returning the proper id. You could step through with the debugger and see if it's returning a proper value.

    Maybe you could try calling the id specifically...

    layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.list);
    
    0 讨论(0)
  • 2020-12-17 11:40

    To perform it, use predefined view ID or declare one. In values folder create ids.xml then add a Item like this:

    <item name="imageViewID" type="id"/>
    

    use this id in your code where you are creating new Instance of view like this:

    RelativeLayout layout=new RelativeLayout(context);
    
    ImageView imageView = new ImageView(context);
    imageView.setId(R.id.imageViewID);
    
    RelativeLayout.LayoutParams layoutParams = new LayoutParams(50, 50);
    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    layout.addView(imageView, layoutParams);
    
    TextView  textView = new TextView(context);
    
    RelativeLayout.LayoutParams textViewParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    textViewParams.addRule(RelativeLayout.BELOW, imageView.getId());
    layout.addView(nameView, nameLayoutParams);
    

    or we can directly use this function View.generateViewId() to perform the same. Like this:

    imageView.setId(View.generateViewId());
    
    0 讨论(0)
提交回复
热议问题