【问题标题】:Is this possible to override screen orientation in fragment这是否可以覆盖片段中的屏幕方向
【发布时间】:2018-05-10 09:42:20
【问题描述】:

我的整个 android 应用程序使用纵向屏幕方向。这就是我在应用程序清单中为我的 Activity 设置 android:screenOrientation="portrait" 的原因。但现在我要实现 YouTubePlayerFragment。

我认为在这个片段中旋转屏幕是非常必要的,这样用户就可以全屏观看 YouTube。

这是否可以在清单中继续使用 screenOrientation="portrait" 并在一个片段中以某种方式覆盖它?有没有不需要为每个片段设置 screenOrientation 的解决方案?

【问题讨论】:

    标签: android youtube screen-orientation


    【解决方案1】:

    在该片段中使用:

    设置方向 - 横向

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    

    设置方向 - 纵向

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    另外,看看这里: Setting the orientation for only 1 fragment in my activity while the rest is in portrait

    【讨论】:

    • 非常感谢 Max。它将为当前活动设置方向,这就是为什么我必须在片段 onDetached() 中调用 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT),对吗?
    • 不仅如此,您还必须在 AndroidManifest 文件中为要处理此方向的活动添加 android:configChanges="orientation" 在活动中使用 onConfigurationChange 覆盖方法。做你想在方向改变中处理的任务。
    猜你喜欢
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2020-02-07
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 2012-07-01
    相关资源
    最近更新 更多