2011-10-09 58 views
4

在Android 2.1模擬器上。
在ActivityInstrumentationtestCase2測試類中,
我聲稱phototButton位於sendButton之上。爲什麼getLocationOnScreen()和getLocationInWindow()返回相同的值?

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

+0

你看看[這個問題](http://stackoverflow.com/questions/2638342/incorrect-coordinates-from-getlocationonscreen-getlocationinwindow)? – Knickedi

+0

重新短語:這些方法返回相同的值是否正常/正常? 我誤解了窗口和屏幕的含義嗎?根據我的理解,它們應該總是返回不同的值。 – user77115

回答

4

我也很困惑,所以我做了一些調查,我總結了 here

基本上,窗口在狀態欄下面(關於z順序而不是y座標)在大多數情況下填充整個屏幕。因此,在正常的活動中,您應該期望這些方法返回相同的值。只有在獨特的情況下,例如窗口實際偏移的對話框,你會看到這些方法返回不同的值。

相關問題