Android Api 8. Get x and y from a View, and Set x and y on a Button

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 03:54:53

you could try getTop() and getLeft()?

I haven't tried this, but I think it will work:

int buttonX = 50; // Arbitrary values - use whatever you want
int buttonY = 100;
int viewX = myView.getLeft();
int viewY = myView.getTop();

Button newButton = new Button();
newButton.setPadding(buttonX - viewX, buttonY - viewY, 0, 0);
// Other button setup

That will set it to an absolute position of (50,100) on the screen. If you want it to be (50,100) relative to the corner of your layout then use this:

int buttonX = 50; // Arbitrary values - use whatever you want
int buttonY = 100;

Button newButton = new Button();
newButton.setPadding(buttonX, buttonY, 0, 0);
// Other button setup

This will only work on layouts that don't automatically position their child content, such as RelativeLayout.

i've recreate my drag and drop using andengine. Works great and better than before

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