【问题标题】:Android how to detect device rotation for portrait configured activity?Android如何检测纵向配置活动的设备旋转?
【发布时间】:2020-05-01 15:50:25
【问题描述】:

我正在开发一个单一的活动应用程序。这意味着我有一个宿主活动和几个不同的片段。在清单中,活动 android:screenOrientation="portrait" 有一个属性来抑制屏幕旋转。但在我的片段之一中,我需要处理设备旋转以启用横向。有没有办法处理特定片段的属性 screenOrientation="portrait" 的配置更改?

【问题讨论】:

标签: android android-fragments android-activity android-manifest


【解决方案1】:

我为这种情况找到了一个优雅的解决方案。 在特定片段中只需设置所需的方向。

   override fun onResume() {
        super.onResume()
        requireActivity().requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
    }

    override fun onPause() {
        super.onPause()
        requireActivity().requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多