【问题标题】:Incorrect Coordinates From getLocationOnScreen/getLocationInWindow来自 getLocationOnScreen/getLocationInWindow 的坐标不正确
【发布时间】:2011-02-07 23:05:52
【问题描述】:

getLocationOnScreen()getLocationInWindow() 的调用都会给我一个top/Y 坐标,该坐标大约是~30px(状态/通知栏的高度)太低了。 left/X 坐标已死。

正如我上面所暗示的,我相信差异是因为状态/通知栏......我可能是错的。如果我可以确定通知栏的大小,我想我可以解决这个问题,但是我无法做到这一点。

任何帮助将不胜感激。

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    我最终通过确定状态/通知栏的高度解决了这个问题,如下所示:

    View globalView = ...; // the main view of my activity/application
    
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int topOffset = dm.heightPixels - globalView.getMeasuredHeight();
    
    View tempView = ...; // the view you'd like to locate
    int[] loc = new int[2]; 
    tempView.getLocationOnScreen(loc);
    
    final int y = loc[1] - topOffset;
    

    【讨论】:

    • 这在正常配置下工作,但是如果在打开 SoftInput/Keyboard 时 Activity 调整 'globalView' 高度,那么键盘高度也会计入 'topOffset',这会导致 'y' 的值错误.
    • int [] locInWindow = new int [2]; globalView.getLocationInWindow(locInWindow); topOffset = locInWindow[1]; //通过获取globalView的Y坐标计算topOffset
    • @ThammeGowda 可以请解释一下我如何在键盘打开时获得顶部偏移
    【解决方案2】:

    我喜欢这样获取状态栏高度,并调整偏移量:

    final int[] location = new int[2];
    anchor.getLocationInWindow(location); // Includes offset from status bar, *dumb*
    Rect anchorRect = new Rect(location[0], location[1],
            location[0] + anchor.getWidth(), location[1] + anchor.getHeight());
    
    anchor.getRootView().findViewById(android.R.id.content).getLocationInWindow(location);
    int windowTopOffset = location[1];
    anchorRect.offset(0, -windowTopOffset);
    

    【讨论】:

    • 为什么窗口中的位置包含从状态栏的偏移量是愚蠢的?您要的是 in window 的位置,那么您还期望什么?
    • 假设窗口位于状态栏下方,因此偏移量不包括状态栏高度。
    • 我认为您将 Window 与 Screen 混淆了。
    • windowTopOffset 还包括操作栏的高度,而不仅仅是状态栏的高度。
    • 这在所有设备上都运行良好。与上述答案相比。
    【解决方案3】:

    我也有同样的问题,试试看

    offset = myView.GetOffsetY();
    

    并按该值调整您的 Y 坐标,例如

    coordY -= offset;
    

    提供 ``-method 的类:

    class MyView extends View {
    
      public int GetOffsetY() {
        int mOffset[] = new int[2];
        getLocationOnScreen( mOffset );
        return mOffset[1]; 
      }
    
    }
    

    【讨论】:

    • 我没有看到“getOffsetY()”。这是什么类的方法?
    • 对不起,我没有发布方法,在类 MyView extends View { public int GetOffsetY() { int mOffset[] = new int[2]; getLocationOnScreen(mOffset);返回 mOffset[1]; }
    【解决方案4】:

    此答案不包括如何获取状态栏高度,但确实解释了 getLocationOnScreen()getLocationInWindow() 返回相同值的行为。

    在正常活动(不是对话框)的情况下,您应该期望这两种方法返回相同的值。窗口位于状态栏下方(如 z 顺序而非 y 坐标),因此这些方法不能用于确定状态栏的高度。

    https://stackoverflow.com/a/20154562/777165

    【讨论】:

    • 在对话框视图中,如果不设置窗口全屏为真,则可以正常工作,但如果设置如下:true则值为不正确,所以需要减去'topOffset'
    【解决方案5】:

    正如@ThammeGowda 所说,正确答案会在显示键盘时给出不正确的位置,但您仍然需要计算操作栏的顶部偏移量,所以直接获取操作栏大小

    fun View.getPosition(): Point {
        val tv = TypedValue()
        if (context.theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            val actionBarHeight =
                TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)
    
            val loc = IntArray(2).apply { getLocationInWindow(this) }
    
            val newY = loc[1] - actionBarHeight - (measuredHeight / 2) // to get the position in the middle of the view
    
            return Point(loc[0], newY)
        }
        return Point(0,0)
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 2019-04-19
      • 2021-10-17
      • 1970-01-01
      相关资源
      最近更新 更多