我如何获得的屏幕尺寸编程在机器人机器人、如何获得、屏幕尺寸

2023-09-11 20:19:45 作者:相关好听个性的>>

Android的屏幕尺寸定义为普通大XLARGE等。

Android defines screen sizes as Normal Large XLarge etc.

它在不同文件夹的静态资源之间自动选择。我需要我的Java code对当前设备此数据。该DisplayMetrics只提供关于当前设备的密度信息。关于屏幕尺寸没有什么用。

It automatically picks between static resources in appropriate folders. I need this data about the current device in my java code. The DisplayMetrics only gives information about the current device density. Nothing is available regarding screen size.

我没有找到屏幕大小枚举中的grep code here 然而,这似乎并没有提供给我的4.0 SDK。有没有一种方式来获得这些信息?

I did find the ScreenSize enum in grep code here However this does not seem available to me for 4.0 SDK. Is there a way to get this information?

推荐答案

复制并粘贴此code到活动,并在执行时它会吐司设备的屏幕尺寸类别。

Copy and paste this code into your Activity and when it is executed it will Toast the device's screen size category.

int screenSize = getResources().getConfiguration().screenLayout &
        Configuration.SCREENLAYOUT_SIZE_MASK;

String toastMsg;
switch(screenSize) {
    case Configuration.SCREENLAYOUT_SIZE_LARGE:
        toastMsg = "Large screen";
        break;
    case Configuration.SCREENLAYOUT_SIZE_NORMAL:
        toastMsg = "Normal screen";
        break;
    case Configuration.SCREENLAYOUT_SIZE_SMALL:
        toastMsg = "Small screen";
        break;
    default:
        toastMsg = "Screen size is neither large, normal or small";
}
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();