Android知识点点滴汇聚

android getLocationOnScreen笔记

2016-11-15  本文已影响116人  幸福的程序媛

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即为状态栏的高度. 这也可以是另一种获取状态栏高度的方法.

上一篇下一篇

猜你喜欢

热点阅读