position view in relativelayout runtime

三世轮回 提交于 2019-12-02 05:36:11

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.

You might want to try setting the position of your button before you add it to the parent view. So just swap these two lines:

relativelayout.addView(button);
button.layout(x, y, x + button.getWidth(), y + button.getHeight());

to be this:

button.layout(x, y, x + button.getWidth(), y + button.getHeight());
relativelayout.addView(button);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!