如何使用代码确定设备屏幕大小类别(小,正常,大,xlarge)?

有什么办法可以确定当前设备的屏幕尺寸类别,如小,正常,大,xlarge?

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

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

例:

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

下面的代码充实了上面的答案,将屏幕大小显示为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(); } 

杰夫吉尔费尔特的答案作为一个静态的帮手方法:

 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"; } } 

感谢上面的答案,这帮了我很多:-)但对于那些(像我一样)被迫仍然支持Android 1.5,我们可以使用Javareflection向下兼容:

 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 ... } 

如果你想很容易地知道Android设备的屏幕密度和大小,你可以使用这个免费的应用程序(无需权限): https : //market.android.com/details?id=com.jotabout.screeninfo

 private String getDeviceResolution() { 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"; } } 

需要检查xlarge屏幕和x ..高密度? 这是从选定的答案改变的代码。

 //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(); } 

这里是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 (); } 

将此代码复制并粘贴到您的Activity ,当它执行时,它将对设备的屏幕大小类别进行烘烤。

 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(); 

难道你不使用string资源和枚举? 您可以定义具有屏幕大小名称的string资源,例如SMALL,MEDIUM或LARGE。 然后,您可以使用string资源的值来创build枚举的实例。

  1. 在您的代码中定义一个Enum,用于您关心的不同屏幕尺寸。

     public Enum ScreenSize { SMALL, MEDIUM, LARGE,; } 
  2. 定义一个string资源,如screenize,其值将是SMALL,MEDIUM或LARGE。

     <string name="screensize">MEDIUM</string> 
  3. screensize的副本放在您关心的每个维度的string资源中。
    例如, <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);