java - 从对话框到 Activity 的按钮位置

标签 java android dialog position screen

我一直在尝试从对话框内的按钮获取位置,以便我可以使用它在 Activity 中的该按钮下方放置一个按钮。

我尝试了一些方法,但似乎都不起作用

int loc1[] = new int[2];
button.getLocationInWindow(loc1);

int loc2[] = new int[2];
button.getLocationOnScreen(loc2);

我也尝试过

button.getX();
button.getY();

该按钮从未放置在对话框中按钮的下方。

(我应该注意,我只提到了获取位置的方法)

有人可以帮助我吗?

谢谢!

最佳答案

按钮放置后需要等待回调。您使用的代码未返回正确的值,因为该值是在放置按钮之前返回的。因此,将按钮添加到高度/宽度为wrap_content的布局中,并使用以下代码:

layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
    getViewTreeObserver().removeGlobalOnLayoutListener(this);

    int[] locations = new int[2];
     layout.getLocationOnScreen(locations);
    int x = locations[0];
    int y = locations[1];
}
});

关于java - 从对话框到 Activity 的按钮位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29307283/

相关文章:

java - 文件系统交互异常: Could not access target file while using documents4j

Java/Android 数学测试 : Got Curious About Int and Float Math/Conversion/Casting

java - 使用 WebDriver 在 Android 上测试 Google Chrome

android - 防止位图被写入磁盘被缩放

具有自定义布局的 Android 对话框 : returning data

java - onKeyDown 仅使用退格键触发

java - 多个 DAO 不完整实现设计

android - 在 Android 项目中使用 Jersey 库

jquery - jquery diglog 插件中有提交 "Enter"选项吗?

forms - 使用 acDialog 打开时的增长形式