为此,我认为您需要将两件事合二为一。
- 一、获取设备屏幕尺寸
- 然后,根据结果,启用或禁用方向。
对于第一部分:
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}
信用:https ://stackoverflow.com/a/11252278/450534 (解决方案在 SO 上很容易获得)
最后,根据上述代码的结果,其中之一:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
或者
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);