首页 > 解决方案 > 获取活动在分屏中的位置

问题描述

如何在分屏中获取活动位置?

我需要检查活动是否在屏幕顶部以显示或隐藏状态栏填充。当设备为横向时,两个应用程序都有状态栏。

我怎样才能检查这个?

标签: androidlayout

解决方案


使用 w 的绝对位置View。您可以检查位置(从上到下)。从您的代码中,找到主要内容View,然后。

public static boolean isOnTopScreen(@NonNull View content) {
   final int[] location = new int[2];
   content.getLocationOnScreen(location);

   return location[1] < 
              (getScreenHeight() / 2);
}

public static int getScreenHeight() {
    return Resources.getSystem()
               .getDisplayMetrics().heightPixels;
}

推荐阅读