是否有办法确定当前设备的屏幕大小的类别,如小,正常,大,xlarge?
不是密度,而是屏幕尺寸。
是否有办法确定当前设备的屏幕大小的类别,如小,正常,大,xlarge?
不是密度,而是屏幕尺寸。
当前回答
在2018年,如果你需要Jeff在Kotlin中的回答,下面是:
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")
}
}
其他回答
您可以使用配置。screenLayout位掩码。
例子:
if ((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_LARGE) {
// on a large screen device ...
}
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";
}
}
难道不能使用字符串资源和枚举来做到这一点吗?您可以定义一个字符串资源,该资源具有屏幕大小的名称,例如SMALL、MEDIUM或LARGE。然后,您可以使用string资源的值来创建枚举的实例。
Define an Enum in your code for the different screen sizes you care about. public Enum ScreenSize { SMALL, MEDIUM, LARGE,; } Define a string resource, say screensize, whose value will be either SMALL, MEDIUM, or LARGE. <string name="screensize">MEDIUM</string> Put a copy of screensize in a string resource in each dimension you care about. For example, <string name="screensize">MEDIUM</string> would go in values-sw600dp/strings.xml and values-medium/strings.xml and <string name="screensize">LARGE</string> would go in sw720dp/strings.xml and values-large/strings.xml. In code, write ScreenSize size = ScreenSize.valueOf(getReources().getString(R.string.screensize);
试试这个函数isLayoutSizeAtLeast(int screenSize)
检查小屏幕,至少320x426 dp及以上 .getConfiguration getresource () () .isLayoutSizeAtLeast (Configuration.SCREENLAYOUT_SIZE_SMALL);
检查正常屏幕,至少320x470 dp及以上 .getConfiguration getresource () () .isLayoutSizeAtLeast (Configuration.SCREENLAYOUT_SIZE_NORMAL);
检查大屏幕,至少480x640 dp及以上 .getConfiguration getresource () () .isLayoutSizeAtLeast (Configuration.SCREENLAYOUT_SIZE_LARGE);
检查超大屏幕,至少720x960 dp及以上 .getConfiguration getresource () () .isLayoutSizeAtLeast (Configuration.SCREENLAYOUT_SIZE_XLARGE);
需要检查x大屏幕和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();
}