android - 如何在 Android 中获取屏幕尺寸,包括虚拟按钮?

标签 android

对于正常使用,您可以使用 DisplayMetrics 类并获得“可渲染”尺寸,但我想计算出实际的物理屏幕尺寸,其中包括虚拟按钮高度。

在我的 Galaxy Nexus 上,无论我尝试什么,报告的尺寸都是 1196x720,我可以用什么来获得 1280x720 的物理尺寸? Nexus 7、Nexus 4 和 Nexus 10 设备也是如此。

更新:这是我现在使用的最终代码,包括修复:

    //Activity A = this;
    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) A.getApplicationContext().getSystemService(Context.WINDOW_SERVICE); 
    Display disp = wm.getDefaultDisplay();

    int API_LEVEL =  android.os.Build.VERSION.SDK_INT;
    if (API_LEVEL >= 17)
    {
        disp.getRealMetrics(displayMetrics);
    }
    else
    {
        disp.getMetrics(displayMetrics);
    }

    int Width = displayMetrics.widthPixels;
    int Height = displayMetrics.heightPixels;

最佳答案

你应该使用 Display.getRealSize(Point)来自官方文档 here

The display area is described in two different ways.

The application display area specifies the part of the display that may contain an application window, excluding the system decorations.

The application display area may be smaller than the real display area because the system subtracts the space needed for decor elements such as the status bar. Use the following methods to query the application display area: getSize(Point), getRectSize(Rect) and getMetrics(DisplayMetrics).

The real display area specifies the part of the display that contains content including the system decorations. Even so, the real display area may be smaller than the physical size of the display if the window manager is emulating a smaller display using (adb shell am display-size). Use the following methods to query the real display area: getRealSize(Point), getRealMetrics(DisplayMetrics).

关于android - 如何在 Android 中获取屏幕尺寸,包括虚拟按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16503064/

相关文章:

java - 填充 Spinner 而不使用 Strings.xml 文件

java - Android - 在 Activity 关闭时设置 Activity 结果

android - Nexus 7 (ME370T) 和 android 4.4.2 上的 BLE

android - 如何在android中获取字符的Unicode

android - onResume 方法不适用于 Twitter 实现

java - 如何在 LinearLayout 中将我的 ImageView 与我的 TextView 对齐?

android - 在 Jetpack Compose 中画一条线

android - 有入口和大门的地方

android - 网络上的 WebView 错误::ERR_CONNECTION_REFUSED

android - 在 onCreate() 中在两个 xml 文件之间进行选择,可能吗?