android getLocationOnScreen笔记

getLocationOnScreen方法获取到的是View左上顶点在屏幕中的绝对位置.(屏幕范围包括状态栏).

 final View decorView=getWindow().getDecorView();
    final int[] decorViewLocationInScreen=new int[2];
    decorView.post(new Runnable() {
        @Override
        public void run() {
            LogUtil.d("---status bar height is "+getStatusBarHeight(FanzhuanActivity.this));
            decorView.getLocationOnScreen(decorViewLocationInScreen);
            LogUtil.d("---decorView locationInScreen x is "+decorViewLocationInScreen[0]+"                              location In Screen Y is "+decorViewLocationInScreen[1]);
        }
    });

获取到的x,y值分别为0,0

我们写的activity或者fragment的布局文件里面最外层的view,去获取他的getLocationInScreen,x为0,y即为状态栏的高度. 这也可以是另一种获取状态栏高度的方法.

你可能感兴趣的:(android getLocationOnScreen笔记)