gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:47 29 4
gpt4 key购买 nike

在 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() 返回相同的值?

最佳答案

我也被这个弄糊涂了,所以我做了一些调查总结一下 here .

基本上,窗口布局在状态栏下方(在 z 轴顺序而非 y 坐标下方),并且在大多数情况下会填满整个屏幕。因此,在正常 Activity 中,您应该期望这些方法返回相同的值。只有在特殊情况下,例如窗口实际偏移的对话框,您才会看到这些方法返回不同的值。

关于android - 为什么 getLocationOnScreen() 和 getLocationInWindow() 返回相同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7706042/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com