view.getLocationOnScreen(locations)返回错误的Y坐标

时间:2014-08-25 21:44:08

标签: android

int[] locations = new int[2];
view.getLocationOnScreen(locations);
int x = locations[0];
int y = locations[1];
otherView.setLocation(x, y);

这会导致otherView垂直〜在第一个视图中间的某个位置而不是它的顶部。

这基本上是setLocation方法的逻辑:

final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.setContentView(R.layout.custom_dialog);
dialog.getWindow().setBackgroundDrawable(
            new ColorDrawable(Color.TRANSPARENT));

WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.x = mX;
layoutParams.y = mY;
dialog.getWindow().setAttributes(layoutParams);

return dialog;

1 个答案:

答案 0 :(得分:0)

只是一个猜测,但我注意到你没有确保默认生成的对话框的边距为零。也许试试这个:

    WindowManager.LayoutParams lp...

    lp.horizontalMargin = 0;
    lp.verticalMargin = 0;