为什么getLocationOnScreen()和getLocationInWindow()返回相同的值?

4
在Android 2.1模拟器中,在ActivityInstrumentationTestCase2测试类中,我正在断言phototButton在sendButton之上。
@UiThreadTest public void testViewLocationOnScreen() {
        // Trying to trigger layout
        activity.findViewById(R.id.rootSnap).forceLayout();
        activity.findViewById(R.id.rootSnap).requestLayout();
        activity.photoButton.getRootView().requestLayout();
        activity.photoButton.requestLayout();
        activity.photoButton.invalidate();
        activity.onWindowFocusChanged(true);  

        // Successfull asserts
        assertTrue(activity.hasWindowFocus());
        ViewAsserts.assertOnScreen(activity.photoButton.getRootView(), activity.photoButton);
        ViewAsserts.assertOnScreen(activity.sendButton.getRootView(), activity.sendButton);
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        Assert.assertTrue(activity.photoButton.isShown());
        Assert.assertTrue(activity.sendButton.isShown());

        // Unexpected screen coordinates returned from 
        // getLocationOnScreen() and getLocationInWindow()
        int[] above = new int[2];
        activity.photoButton.getLocationOnScreen(above);
        int[] below = new int[2];
        activity.sendButton.getLocationOnScreen(below);
        log("getLocationOnScreen-above", above);
        log("getLocationOnScreen-below", below);
        // Logs screen coodinates [0, 76] and [0, 178]

        above = new int[2];
        activity.photoButton.getLocationInWindow(above);
        below = new int[2];
        activity.sendButton.getLocationInWindow(below);
        log("getLocationInWindow-above", above);
        log("getLocationInWindow-below", below);
        // Logs window coordinates [0, 76] and [0, 178]
    }

我原本期望这些方法返回不同的值。

为什么getLocationOnScreen()和getLocationInWindow()返回相同的值?


你看过这个问题了吗? - Knickedi
重述:这些方法返回相同的值是否正常/合理?我对Window和Screen的意义有误解吗? 据我理解,它们应该始终返回不同的值。 - user77115
1个回答

4
我也对此感到困惑,因此我进行了一些调查并在这里进行了总结。
基本上,窗口位于状态栏之下(与 y 坐标无关的 Z 轴顺序),并且在大多数情况下填充整个屏幕。 因此,在正常活动中,您应该期望这些方法返回相同的值。 只有在唯一的情况下,例如窗口实际上被偏移的对话框中,您才会看到这些方法返回不同的值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接