Remove orientation restricitons programmatically

2020-08-09 17:03发布

In my manifest I've setup an activity restricted to portrait orientation. But I need to remove this restriction on condition. So, how do I achieve removing orientation restrictions programmatically ?

upd: my present settings are:

    <activity
        android:name=".activity.MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar"
        android:screenOrientation="portrait"
        android:configChanges="orientation">


 /**
 * Defines whether the device being used is a tablet and if so adds horizontal orientation option.
 */
     protected void _updateScreenOrientationModes(){
         if(((MyApplication) getApplication())._isTablet == true)
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
         }

3条回答
可以哭但决不认输i
2楼-- · 2020-08-09 17:39

I'm going to leave my other answer, as it addresses the more general question that you asked. However, in a comment to someone else you said:

I need to allow tablets to have both orientations and handsets only portrait

Your particular case is actually easier than the general case: Remove both android:screenOrientation="portrait" and android:configChanges="orientation" from your Manifest to allow the default behavior. Then, during startup, if the device is a handset, force portrait orientation with

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

A device will obviously never change between being a tablet or a handset at runtime, so you only need to do this once, at startup. Tablets will get the default behavior, so whatever physical orientation the device is in, that's what they'll use. Handsets will be forced into portrait and stay that way.

查看更多
冷血范
3楼-- · 2020-08-09 17:46

Programmatically you can change your screen orientations by using "setRequestedOrientation()"

In your java class write the following code as per your condition required.....

To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE constant:

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT constant:

   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
查看更多
叼着烟拽天下
4楼-- · 2020-08-09 17:57

Whether or not you've set android:screenOrientation in your Manifest, you can programmatically set the orientation with Activity.setRequestedOrientation().

In essence, "removing the restriction" is accomplished by

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

after which the Activity will exhibit the default behavior of changing screen orientation when the physical device orientation changes.

It's likely, however, that you want to actually match the current physical device orientation at the same time. I haven't tried it, but I'm pretty sure that if all you did was the above, if the device was physically in a landscape orientation when you did it, then you'd stay in portrait mode until you physically moved the device to portrait and back to landscape.

What I would do is be sure to set android:configChanges="orientation" in your Manifest and override Activity.onConfigurationChanged(), in which you can, according to your condition, either perform the orientation change or cache the orientation. Then whenever your condition changes, you'll have the current physical orientation handy so you can change it at that point if necessary.

查看更多
登录 后发表回答