如何使用代码确定设备屏幕尺寸类别(小,标准,大,xlarge)?


308

有什么方法可以确定当前设备的屏幕尺寸类别,例如小,普通,大,xlarge?

不是密度,而是屏幕尺寸。

Answers:


420

您可以使用Configuration.screenLayout位掩码。

例:

if ((getResources().getConfiguration().screenLayout & 
    Configuration.SCREENLAYOUT_SIZE_MASK) == 
        Configuration.SCREENLAYOUT_SIZE_LARGE) {
    // on a large screen device ...

}

31
要进行x大型检测,请确保在项目中使用目标android-3.0。或将静态值4用于x-large。
Peterdk 2011年

5
某些设备的屏幕大小可能不确定,因此也可以使用Configuration.SCREENLAYOUT_SIZE_UNDEFINED进行检查。
valerybodak

您能否使用> =来放大或放大屏幕?
安德鲁·S

150

下面的代码完善了上面的答案,将屏幕尺寸显示为Toast。

//Determine screen size
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
    Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
    Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG).show();
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
    Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG).show();
}
else {
    Toast.makeText(this, "Screen size is neither large, normal or small", Toast.LENGTH_LONG).show();
}

以下代码将屏幕密度显示为Toast。

//Determine density
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;

if (density == DisplayMetrics.DENSITY_HIGH) {
    Toast.makeText(this, "DENSITY_HIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
}
else if (density == DisplayMetrics.DENSITY_MEDIUM) {
    Toast.makeText(this, "DENSITY_MEDIUM... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
}
else if (density == DisplayMetrics.DENSITY_LOW) {
    Toast.makeText(this, "DENSITY_LOW... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
}
else {
    Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW.  Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
}

吐司很适合设计。
MinceMan 2011年

有人可以确认特大号吗?
内森·H

68

Jeff Gilfelt作为静态助手方法答案:

private static String getSizeName(Context context) {
    int screenLayout = context.getResources().getConfiguration().screenLayout;
    screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;

    switch (screenLayout) {
    case Configuration.SCREENLAYOUT_SIZE_SMALL:
        return "small";
    case Configuration.SCREENLAYOUT_SIZE_NORMAL:
        return "normal";
    case Configuration.SCREENLAYOUT_SIZE_LARGE:
        return "large";
    case 4: // Configuration.SCREENLAYOUT_SIZE_XLARGE is API >= 9
        return "xlarge";
    default:
        return "undefined";
    }
}

12
private String getDeviceDensity() {
    int density = mContext.getResources().getDisplayMetrics().densityDpi;
    switch (density)
    {
        case DisplayMetrics.DENSITY_MEDIUM:
            return "MDPI";
        case DisplayMetrics.DENSITY_HIGH:
            return "HDPI";
        case DisplayMetrics.DENSITY_LOW:
            return "LDPI";
        case DisplayMetrics.DENSITY_XHIGH:
            return "XHDPI";
        case DisplayMetrics.DENSITY_TV:
            return "TV";
        case DisplayMetrics.DENSITY_XXHIGH:
            return "XXHDPI";
        case DisplayMetrics.DENSITY_XXXHIGH:
            return "XXXHDPI";
        default:
            return "Unknown";
    }
}

1
这将获得屏幕的密度。问题指定“不是密度,而是屏幕尺寸”。
斯巴鲁Tashiro '18

11

感谢上面的回答,这对我大有帮助:-)但是对于那些仍然仍然支持Android 1.5的人(如我),我们可以使用Java反射来实现向后兼容:

Configuration conf = getResources().getConfiguration();
int screenLayout = 1; // application default behavior
try {
    Field field = conf.getClass().getDeclaredField("screenLayout");
    screenLayout = field.getInt(conf);
} catch (Exception e) {
    // NoSuchFieldException or related stuff
}
// Configuration.SCREENLAYOUT_SIZE_MASK == 15
int screenType = screenLayout & 15;
// Configuration.SCREENLAYOUT_SIZE_SMALL == 1
// Configuration.SCREENLAYOUT_SIZE_NORMAL == 2
// Configuration.SCREENLAYOUT_SIZE_LARGE == 3
// Configuration.SCREENLAYOUT_SIZE_XLARGE == 4
if (screenType == 1) {
    ...
} else if (screenType == 2) {
    ...
} else if (screenType == 3) {
    ...
} else if (screenType == 4) {
    ...
} else { // undefined
    ...
}

2
您可以定位平台的最新版本,并从Configuration类中引用常量。这些是静态最终值,将在编译时内联(即,它们将被其实际值替换),因此您的代码在平台的旧版本上不会中断。
卡拉库里2012年

尼斯,我不知道...您是在说android:targetSdkVersion吗?
A. Masson

1
是的,您将如何定位特定版本。大多数人(至少我所看到的)将其设置targetSdkVersion为最新版本。
Karakuri

9

如果您想轻松了解Android设备的屏幕密度和大小,可以使用此免费应用程序(无需许可):https : //market.android.com/details?id=com.jotabout.screeninfo


3
这个问题与特定设备无关,而与多个设备配置文件的编程有关(这是为移动平台开发时的重要软件开发过程)。
mtmurdock 2012年

1
市场上有不错的已知应用程序可用-也很高兴看到该应用程序用来提供其信息的代码
Stan Kurdziel 2012年

4
@StanKurdziel源代码在MIT开源许可证下发布,可在以下网址
mmathieum,2012年

此链接现在已失效
Vadim Kotov

5

是否需要检查xlarge屏幕和x..high密度?这是所选答案中更改的代码。

//Determine screen size
if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {     
    Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
} else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {     
    Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show();
} else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {     
    Toast.makeText(this, "Small sized screen" , Toast.LENGTH_LONG).show();
} else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {     
    Toast.makeText(this, "XLarge sized screen" , Toast.LENGTH_LONG).show();
} else {
    Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}

//Determine density
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;

if (density==DisplayMetrics.DENSITY_HIGH) {
    Toast.makeText(this, "DENSITY_HIGH... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_MEDIUM) {
    Toast.makeText(this, "DENSITY_MEDIUM... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_LOW) {
    Toast.makeText(this, "DENSITY_LOW... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_XHIGH) {
    Toast.makeText(this, "DENSITY_XHIGH... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_XXHIGH) {
    Toast.makeText(this, "DENSITY_XXHIGH... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_XXXHIGH) {
    Toast.makeText(this, "DENSITY_XXXHIGH... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
} else {
    Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW.  Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
}

[密度]在这种情况下,您需要活动。如果在外面使用getWindowManager(),则此代码WindowManager windowManager =(WindowManager)context .getSystemService(Context.WINDOW_SERVICE);
horkavlna,2016年

3

这是Tom McFarlin的答案的Xamarin.Android版本

        //Determine screen size
        if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeLarge) {
            Toast.MakeText (this, "Large screen", ToastLength.Short).Show ();
        } else if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeNormal) {
            Toast.MakeText (this, "Normal screen", ToastLength.Short).Show ();
        } else if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeSmall) {
            Toast.MakeText (this, "Small screen", ToastLength.Short).Show ();
        } else if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeXlarge) {
            Toast.MakeText (this, "XLarge screen", ToastLength.Short).Show ();
        } else {
            Toast.MakeText (this, "Screen size is neither large, normal or small", ToastLength.Short).Show ();
        }
        //Determine density
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager.DefaultDisplay.GetMetrics (metrics);
        var density = metrics.DensityDpi;
        if (density == DisplayMetricsDensity.High) {
            Toast.MakeText (this, "DENSITY_HIGH... Density is " + density, ToastLength.Long).Show ();
        } else if (density == DisplayMetricsDensity.Medium) {
            Toast.MakeText (this, "DENSITY_MEDIUM... Density is " + density, ToastLength.Long).Show ();
        } else if (density == DisplayMetricsDensity.Low) {
            Toast.MakeText (this, "DENSITY_LOW... Density is " + density, ToastLength.Long).Show ();
        } else if (density == DisplayMetricsDensity.Xhigh) {
            Toast.MakeText (this, "DENSITY_XHIGH... Density is " + density, ToastLength.Long).Show ();
        } else if (density == DisplayMetricsDensity.Xxhigh) {
            Toast.MakeText (this, "DENSITY_XXHIGH... Density is " + density, ToastLength.Long).Show ();
        } else if (density == DisplayMetricsDensity.Xxxhigh) {
            Toast.MakeText (this, "DENSITY_XXXHIGH... Density is " + density, ToastLength.Long).Show ();
        } else {
            Toast.MakeText (this, "Density is neither HIGH, MEDIUM OR LOW.  Density is " + density, ToastLength.Long).Show ();
        }

2
请不要只是转储一些代码,而是说明您做了什么以及如何做
David Medenjak

2

试试这个函数isLayoutSizeAtLeast(int screenSize)

要检查小屏幕,请至少达到320x426 dp及以上 getResources()。getConfiguration()。isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_SMALL);

要检查正常屏幕,请至少达到320x470 dp及以上 getResources()。getConfiguration()。isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_NORMAL);

要检查大屏幕,请至少达到480x640 dp及以上 getResources()。getConfiguration()。isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE);

要检查超大屏幕,请至少720x960 dp及更高版本 getResources()。getConfiguration()。isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_XLARGE);


真好!该方法最早也可以在API级别11上使用!
Enselic

2

在2018年,如果您需要在Kotlin中获得Jeff的答案,则为:

  private fun determineScreenSize(): String {
    // Thanks to https://stackoverflow.com/a/5016350/2563009.
    val screenLayout = resources.configuration.screenLayout
    return when {
      screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_SMALL -> "Small"
      screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_NORMAL -> "Normal"
      screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_LARGE -> "Large"
      screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_XLARGE -> "Xlarge"
      screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_UNDEFINED -> "Undefined"
      else -> error("Unknown screenLayout: $screenLayout")
    }
  }

1

您不能使用字符串资源和枚举来执行此操作吗?您可以定义一个具有屏幕大小名称的字符串资源,例如SMALL,MEDIUM或LARGE。然后,您可以使用字符串资源的值来创建枚举的实例。

  1. 在代码中为您关心的不同屏幕尺寸定义一个枚举。

    public Enum ScreenSize {
        SMALL,
        MEDIUM,
        LARGE,;
    }
  2. 定义一个字符串资源,例如screensize,其值将为SMALL,MEDIUM或LARGE。

    <string name="screensize">MEDIUM</string>
  3. screensize在您关心的每个维度中,在字符串资源中放置一个副本。
    例如,<string name="screensize">MEDIUM</string>将进入values-sw600dp / strings.xml和values-medium / strings.xml,<string name="screensize">LARGE</string>进入sw720dp / strings.xml和values-large / strings.xml。
  4. 用代码写
    ScreenSize size = ScreenSize.valueOf(getReources().getString(R.string.screensize);

这真是棒极了...但是,我已经使用3种设备进行了测试,并且当我期望较大时,平板电脑仍会返回小。我的string.xml文件在values-h1024dp,values-h700dp和values-h200dp中定义,并带有相应的<string name =“ screensize”> xxxxxx </ string>
A. Masson

1

复制此代码并将其粘贴到您的代码中Activity,执行该代码后,它将Toast显示设备的屏幕尺寸类别。

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();
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.