是否有办法确定当前设备的屏幕大小的类别,如小,正常,大,xlarge?
不是密度,而是屏幕尺寸。
是否有办法确定当前设备的屏幕大小的类别,如小,正常,大,xlarge?
不是密度,而是屏幕尺寸。
当前回答
难道不能使用字符串资源和枚举来做到这一点吗?您可以定义一个字符串资源,该资源具有屏幕大小的名称,例如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);
其他回答
需要检查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();
}
难道不能使用字符串资源和枚举来做到这一点吗?您可以定义一个字符串资源,该资源具有屏幕大小的名称,例如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);
下面的代码充实了上面的答案,将屏幕大小显示为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();
}
试试这个函数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);
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";
}
}