当前位置: 首页 > 知识库问答 >
问题:

获取Android中的屏幕高度

商琛
2023-03-14

在Android系统中如何获得屏幕的可用高度?我需要的高度减去状态栏/菜单栏或任何其他可能在屏幕上的装饰,我需要它为所有设备工作。另外,我需要在onCreate函数中了解这一点。我知道这个问题以前有人问过,但我已经尝试过他们的解决方案,但没有一个奏效。以下是我尝试过的一些事情:

我已经在API7-17上测试了这段代码。不幸的是,在API 13的底部水平和垂直都有额外的空间,而在API 10、8和7的底部水平和垂直都没有足够的空间。(我没有在过时的API上进行测试):

Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;

TypedValue tv = new TypedValue();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
    if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
        screenHeight -= TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}

int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
    screenHeight -= getResources().getDimensionPixelSize(resourceId);
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;
Point size = new Point();
getWindowManager().getDefaultDisplay().getRealSize(size);
screenWidth = size.x;
screenHeight = size.y;
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
// since SDK_INT = 1;
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
try
{
    // used when 17 > SDK_INT >= 14; includes window decorations (statusbar bar/menu bar)
    screenWidth = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
    screenHeight = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
}
catch (Exception ignored)
{
    // Do nothing
}
try
{
    // used when SDK_INT >= 17; includes window decorations (statusbar bar/menu bar)
    Point realSize = new Point();
    Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
    screenWidth = realSize.x;
    screenHeight = realSize.y;
}
catch (Exception ignored)
{
    // Do nothing
}
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
    result = getResources().getDimensionPixelSize(resourceId);
screenHeight -= result;
result = 0;
if (screenHeight >= screenWidth)
    resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
else
    resourceId = getResources().getIdentifier("navigation_bar_height_landscape", "dimen", "android");
if (resourceId > 0)
    result = getResources().getDimensionPixelSize(resourceId);
screenHeight -= result;

在API17上,它可以正确地计算状态栏和菜单栏的高度,但在纵向中却不能计算状态栏和菜单栏的高度。在API10上,它返回0。我需要它工作在所有设备或最低API 7的理想状态。

共有1个答案

高祺
2023-03-14

工作解决方案

创建两个虚拟视图:

 <View
    android:id="@+id/top"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_alignParentTop="true" />

<View
        android:id="@+id/bottom"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true" />

现在,您应该对这两个视图调用getLocationOnScreen(int[])以接收它们的位置。这个函数返回一个由2个整数组成的数组。第一个是x,第二个是Y。我们只需要y。还要注意,只有在创建所有视图并退出oncreate方法之后,才返回正确的值。

final View top = (View) view.findViewById(R.id.top);
    final View bottom = (View) view.findViewById(R.id.bottom);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            int topLoc[] = new int[2];
            top.getLocationOnScreen(topLoc);
            int BottomLoc[] = new int[2];
            bottom.getLocationOnScreen(BottomLoc);
            Log.d(Constants.debug, "topY: "+ topLoc[1]+" BottomY:" + BottomLoc[1]);
        }
    },4000);
07-05 02:28:23.367      565-565/com.torcellite.xx D/xx: topY: 50 BottomY:1182
07-05 02:29:17.360      707-707/com.torcellite.xx D/xx: topY: 0 BottomY:1280

你现在要做的就是减法。

 类似资料:
  • 问题内容: 如何获取Android屏幕的可用高度?我需要减去状态栏/菜单栏或屏幕上可能显示的任何其他装饰的高度,并且需要它适用于所有设备。另外,我需要在onCreate函数中知道这一点。我知道之前曾有人问过这个问题,但我已经尝试过他们的解决方案,但没有一个起作用。这是我尝试过的一些方法: 我已经在API 7-17上测试了此代码。不幸的是,在API 13的水平和垂直底部都没有多余的空间,而在API

  • 问题内容: 我想获取运行我的应用程序的设备的实际屏幕高度。为此,我尝试以下操作: 我使用模拟器运行的设备的屏幕宽度为1080像素,屏幕高度为1920像素。宽度可以正确显示(1080像素),但根据应用程序,高度仅为1776像素。我需要什么来使我的应用显示正确的屏幕高度?是让屏幕高度的好方法? 问题答案: 看到这个答案 如果您的API级别> 13,请在活动时尝试 如果您不在活动中,则可以通过WINDO

  • 在Android系统中如何获得屏幕的可用高度?我需要的高度减去状态栏/菜单栏或任何其他可能在屏幕上的装饰,我需要它为所有设备工作。另外,我需要在onCreate函数中了解这一点。我知道这个问题以前有人问过,但我已经尝试过他们的解决方案,但没有一个奏效。以下是我尝试过的一些事情: 这还没有考虑状态栏/菜单栏: 我然后用下面的代码从屏幕高度中减去状态栏和菜单栏的高度: 在API17上,它给出了正确计算

  • 因为有两种方法可以获得尺寸。从13级以上的API来看,这种方法对我很有效 14岁以下,这种方法对我有效 我可以得到所有API级别的屏幕大小吗???请帮帮我。

  • 本文向大家介绍Android 获取屏幕的多种宽高信息的示例代码,包括了Android 获取屏幕的多种宽高信息的示例代码的使用技巧和注意事项,需要的朋友参考一下 本文主要介绍了Android 获取屏幕的多种宽高信息的示例代码,分享给大家,具体如下: 包含的宽高信息如下图所示: 在模拟器上获取到的数据: 08-26 07:19:32.712 7834-7834/com.czy.screeninfo E

  • 本文向大家介绍使用Tkinter Python获取屏幕的高度和宽度,包括了使用Tkinter Python获取屏幕的高度和宽度的使用技巧和注意事项,需要的朋友参考一下 Tkinter是为Python程序提供GUI编程功能的库。作为GUI创建的一部分,我们需要创建不同大小和深度的屏幕布局。在此程序中,我们将看到如何以像素和毫米为单位计算屏幕尺寸。我们还可以获得以像素为单位的屏幕深度。作为Tkinte