首页 > 解决方案 > Android:如何同时支持用户方向和从活动中手动请求的方向

问题描述

我已将screenOrientationin manifest 指定为user. 我的活动中有一个按钮,每当用户单击该按钮时,我都会请求更改方向:纵向到横向,反之亦然。问题是,在我使用以下命令请求方向后:

requireActivity().requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

活动停止发送配置更改事件,因为方向user不再。有什么方法可以同时监听方向变化和手动请求方向变化?

标签: android

解决方案


尝试以下解决方案;-

if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        else if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

推荐阅读