【问题标题】:How to get Button's onscreen coordinates如何获取屏幕坐标上的按钮
【发布时间】:2017-05-16 09:07:19
【问题描述】:

我正在尝试获取按钮的坐标,以便在单击时使用该按钮设置对话框的位置。我已经将按钮放置在 LinearLayout 中(这是按钮的父布局),所以我使用下面的代码来获取坐标

x = b1.getX();
y = b1.getY();

但上面的代码给出了与该 LinearLayout 相关的坐标,我想要按钮的坐标,它位于 Main Parent 布局中。我搜索了它,我没有得到正确的答案,请帮助,提前谢谢

【问题讨论】:

    标签: android android-studio button coordinates android-button


    【解决方案1】:

    像这样使用方法View.getLocationOnScreen(int[] location)

    int locations[] = new int[2];
    
    view.getLocationOnScreen(locations);
    

    locations[0] 将具有 x 坐标和 location[1] y 坐标。

    【讨论】:

      【解决方案2】:

      使用View.getLocationOnScreen() 和/或getLocationInWindow()

      您只能在布局发生后调用它。您在视图定位在屏幕上之前调用该方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-22
        • 2015-08-01
        • 2016-02-29
        • 1970-01-01
        • 1970-01-01
        • 2016-03-19
        • 2012-02-02
        • 1970-01-01
        相关资源
        最近更新 更多