如何在不锁定活动方向的情况下锁定片段方向? [英] How to lock fragment orientation without locking activity orientation?

查看:88
本文介绍了如何在不锁定活动方向的情况下锁定片段方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定的用例,我希望将片段锁定为纵向模式,但仍要旋转活动(和/或在同一活动中可见的其他片段).可以这样做吗?

I have a specific use case where I want a fragment to be locked in portrait mode, but still rotate the activity (and/or other fragments visible in the same activity). Is it possible to do that?

所有锁定片段方向的解决方案都建议使用setRequestedOrientation并锁定活动方向,但是我需要其他可见的片段进行旋转.

All the solutions to locking a fragment orientation suggest to use setRequestedOrientation and lock the activity orientation, but I need other visible fragments to rotate.

我的应用程序支持API 10+(如果有使用API​​ 11+的不错的解决方案,我可能会考虑取消对API< 11中的风景的支持).

My app supports API 10+ (if there is a nice solution that uses API 11+ I may consider removing support for landscape in API <11).

谢谢.

推荐答案

看看这个答案:

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

Override setUserVisibleHint() in each fragment.

在肖像中只有片段:

@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.

回答者: https://stackoverflow.com/a/13252788/2767703

这篇关于如何在不锁定活动方向的情况下锁定片段方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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