首页 文章

获取报告的屏幕宽度和高度

提问于
浏览
0

这可能最终成为“是”或“否”的问题......

从Nougat(也许是棉花糖?)开始,用户可以更改设备的显示尺寸 . 特别是在Galaxy S8和S8上,用户可以改变显示分辨率 .

我在这里看到了解决方案:Get Screen width and height

问题:这些解决方案是否提供了屏幕的实际高度/宽度,无论分辨率如何变化,或者如果分辨率发生变化,它们是否会给出调整后的屏幕高度/宽度?

如果是第一个,我如何获得调整后的高度/宽度?

1 回答

  • 2

    有趣的问题 . 它为我提供了像素的调整尺寸 . 我现在在运行Android O的Pixel中测试过它 .

    方法1:

    DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int height = displayMetrics.heightPixels;
            int width = displayMetrics.widthPixels;
    

    方法2:

    Configuration configuration = getResources().getConfiguration();
            int screenWidthDp = configuration.screenWidthDp; 
            int screenHeightDp = configuration.screenHeightDp;
            int smallestScreenWidthDp = configuration.smallestScreenWidthDp;
    

    两种方法都返回调整后的尺寸 . 第一种方法返回设备屏幕尺寸 . 第二种方法返回应用程序屏幕的尺寸 . 当在多窗口模式下打开两个应用程序时,这两个结果会有所不同 .

相关问题