以编程方式删除方向限制

use*_*991 14 android android-layout

在我的清单中,我设置了一个仅限于纵向的活动.但是我需要在条件上删除这个限制.那么,我如何以编程方式实现删除方向限制?

upd:我目前的设置是:

    <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);
         }
Run Code Online (Sandbox Code Playgroud)

Dar*_*tle 19

无论您是否设置android:screenOrientation了Manifest,都可以使用Activity.setRequestedOrientation()以编程方式设置方向.

实质上,"消除限制"是通过以下方式完成的

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Run Code Online (Sandbox Code Playgroud)

之后,当物理设备方向发生变化时,Activity将显示更改屏幕方向的默认行为.

但是,您可能希望实际匹配当前的物理设备方向.我没有尝试过,但我很确定如果您所做的就是上述内容,如果设备实际上处于横向状态,那么您将保持纵向模式直到您实际移动设备画像和回到风景.

我要做的是确保android:configChanges="orientation"在你的Manifest中设置并覆盖Activity.onConfigurationChanged(),你可以根据你的情况执行方向更改或缓存方向.然后,只要您的条件发生变化,您就可以获得当前的物理方向,以便在必要时可以在此时更改它.