position view in relativelayout runtime

前端 未结 2 885
余生分开走
余生分开走 2021-01-27 01:31

i want to add a view to a relativelayout. this view must be added to a specified position.

i have this:

    int x = ...;
    int y = ...;
    button.setO         


        
2条回答
  •  暖寄归人
    2021-01-27 02:01

    i have a workaround.

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    lp.leftMargin = x;
    lp.topMargin = y;
    relativelayout.addView(tmpTextButtonView, lp);
    

    it is not great to use margin for positioning floating views. however it works for me.

提交回复
热议问题