尝试使用这个
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthInPixels = metrics.widthPixels;
int heightInPixels = metrics.heightPixels;
if (widthInPixels > 600 || heightInPixels > 600) {
// This is a tablet dont run
}
或者
if(tabletSize() > 6)
Log.d("Testing", "Is Tablet ");
else
Log.d("Testing", "Not a Tablet ");
public double tabletSize() {
double size = 0;
try {
// Compute screen size
DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics();
float screenWidth = dm.widthPixels / dm.xdpi;
float screenHeight = dm.heightPixels / dm.ydpi;
size = Math.sqrt(Math.pow(screenWidth, 2) +
Math.pow(screenHeight, 2));
} catch (Exception e) {
Log.e("Testing", "Exception " + e);
}
return size;
}
这将为您提供以英寸为单位的设备尺寸,因此您可以将其限制为特定的屏幕尺寸(通常一个表格大于 6)