如何在小屏幕布局上禁用景观



我允许对我的应用程序进行所有可能的方向,我只是不喜欢它看起来如此,我想做的是禁用小型布局的景观。有没有办法做到这一点 ?

我发现的只是在清单文件上要做的更改,但我相信,通过重新配置清单,我将把更改应用于所有布局。

最简单的方法是将其放入您所有活动的onCreate()方法中(更好的是,将其放入基本阶段,并将您的所有活动扩展到其中)

@Override
protected void onCreate(Bundle bundle) {
   super.onCreate(bundle);
   if (isLargeDevice(getBaseContext())) {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
   } else {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
   }
}

您可以使用此方法检测设备是手机还是平板电脑:

private boolean isLargeDevice(Context context) {
        int screenLayout = context.getResources().getConfiguration().screenLayout;
        screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;
        switch (screenLayout) {
        case Configuration.SCREENLAYOUT_SIZE_SMALL:
        case Configuration.SCREENLAYOUT_SIZE_NORMAL:
            return false;
        case Configuration.SCREENLAYOUT_SIZE_LARGE:
        case Configuration.SCREENLAYOUT_SIZE_XLARGE:
            return true;
        default:
            return false;
        }
    }

检查此链接您可以检查设备的类型并根据需要设置定向

Android:允许平板电脑的肖像和景观,但在电话上强制肖像?

您可以在程序上处理运行时配置更改

在您的清单中:

    <activity android:name=".MyActivity"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">

在您的活动中

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
     ///check the screen size and change it to potrait
    }
}

或检查此答案以查看如何检查屏幕尺寸并更改

例如480屏幕尺寸设备:应用于OnCreate方法:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
if(width==480){
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

最新更新