允许在一个片段中旋转/横向 [英] Allow rotation/landscape in one fragment

查看:15
本文介绍了允许在一个片段中旋转/横向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个带有四个片段的 FragmentPagerAdapter 的 Activity(使用 ViewPagerIndicator 库).其中一个片段有单独的纵向和横向布局设计,其他三个没有并且需要固定为纵向.

My app has a single Activity with a FragmentPagerAdapter with four fragments (Using the ViewPagerIndicator library). One of these fragments has designs for both a separate portrait and landscape layout, the other three do not and need to be fixed to portrait orientation.

我的想法是在清单中设置 android:configChanges="orientation" 并在 onResume()<中调用 getActivity().setRequestedScreenOrientation()/code> 的所有片段,在其中三个片段中锁定到 SCREEN_ORIENTATION_PORTRAIT,但在需要允许旋转的片段中锁定到 SCREEN_ORIENTATION_UNSPECIFIED,但这不起作用.该应用程序保持纵向模式.

My thought was to set android:configChanges="orientation" in the manifest and call getActivity().setRequestedScreenOrientation() in the onResume() of all the fragments, locking to SCREEN_ORIENTATION_PORTRAIT in three of them but to SCREEN_ORIENTATION_UNSPECIFIED in the one that needs to allow rotation, but this doesn't work. The app remains in portrait mode.

有没有办法做到这一点?

Is there a way to achieve this?

如果无论如何允许片段在没有其活动的情况下改变方向,那么实际活动实际上不需要旋转,但我没有发现任何提到这是可能的.如果活动旋转也同样可以,因为标签栏在横向时会被隐藏.

It isn't actually necessary for the actual activity to rotate if there is anyway to allow a fragment to change orientation without its activity doing so, but I have not found anything mentioning this being possible. It would also be equally ok if the activity rotates since the tab bar will be hidden when in landscape orientation.

推荐答案

覆盖每个片段中的setUserVisibleHint().

在肖像中只有片段:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser) {
        Activity a = getActivity();
        if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

在肖像/风景片段中:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser) {
        Activity a = getActivity();
        if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
    }
}

这将允许整个活动在一个片段中旋转,但在其他片段中将其固定为纵向.

This will allow the whole activity to rotate in one fragment, but fix it to portrait in others.

这篇关于允许在一个片段中旋转/横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆