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

后端 未结 3 908
执笔经年
执笔经年 2021-01-24 23:20

­I\'m coding with API 8.
I need to get coordinates X and Y from a View and and set them as the coordinates of a new Button.
I tried different ways but nothing works....<

相关标签:
3条回答
  • 2021-01-24 23:59

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

    0 讨论(0)
  • 2021-01-25 00:05

    you could try getTop() and getLeft()?

    0 讨论(0)
  • 2021-01-25 00:05

    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.

    0 讨论(0)
提交回复
热议问题