当前位置: 动力学知识库 > 问答 > 编程问答 >

Android: Replace setX() for lower API's

问题描述:

I am writing a method that draws a layout ontop of buttons. My problem is with the position of the layout that I am creating on lower API's. On newer I am using the setX() and setY() methods but since that is not working on lower I tried with setting the layout params as described here Android - Use of view.setX() and setY in api 8 but I am not getting the results I want. With this

int[] location = new int[2];

button.getLocationOnScreen(location);

int x = location[0];

int y = location[1];

borderRelativeLayout.setX(x);

borderRelativeLayout.setY(y);

viewGroup.addView(borderRelativeLayout)

I am achieving this:

correct position

but if I use this code to support lower API's

relativeLayoutparams.leftMargin = x;

relativeLayoutparams.topMargin = y;

viewGroup.addView(borderRelativeLayout, relativeLayoutparams);

I am getting this as a result: incorrect

Any help would be appreciated.

网友答案:

For API 1:

int[] location = new int[2];
button.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
borderRelativeLayout.offsetLeftAndRight(x - borderRelativeLayout.getLeft());
borderRelativeLayout.offsetTopAndBottom(y - borderRelativeLayout.getTop());

EDIT:
Did you consider a background drawable for desired effect?

分享给朋友:
您可能感兴趣的文章:
随机阅读: