57

我的应用需要处于纵向模式,因此我在清单中通过以下方式设置它:

android:screenOrientation="portrait"

但我最近刚刚添加了另一个片段(FragA),它在横向上的外观和功能都提高了 10 倍。有什么我可以在我的 FragA 中放入的东西,只是在横向中制作该片段,同时将应用程序的其余部分保留为纵向,或者通过这样做,我是否必须向我的其他片段添加一些东西以保持它们作为纵向保留?

4

8 回答 8

111

在您想要特定(在本例中为纵向)方向的片段中使用以下代码行。

getActivity().setRequestedOrientation(
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

如果您想在片段中确定方向,即基于用户握持设备的方式,请使用以下代码行。

getActivity().setRequestedOrientation(
                ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

希望,这将为您提供预期的解决方案。

于 2013-10-15T15:21:27.217 回答
23

在每个片段中,设置请求的方向。

参考文档:http: //developer.android.com/reference/android/content/pm/ActivityInfo.html

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   // Fragment locked in portrait screen orientation
   getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);    

   // Fragment locked in landscape screen orientation
   getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

   // Fragment screen orientation normal both portait and landscape       
   getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
于 2015-09-05T20:04:26.107 回答
13

方向属性是每个活动的,因此您可以仅为包含片段的活动声明方向,使其处于横向状态,其余活动将保持原样。

于 2012-10-03T07:45:59.100 回答
4

所以我现在正在处理这个问题。我们只有纵向模式应用程序(目前)。但是有一个片段需要在Landscape中。我们正在使用单一活动方法,因此接受的答案对我没有帮助。

我能想到的最快的解决方案就是这个。

private var swappingOrientation = false

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    if(savedInstanceState == null) {
        swappingOrientation = true
        activity?.apply {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }
    }
}

override fun onDestroy() {
    super.onDestroy()

    if(!swappingOrientation) {
        activity?.apply {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
    }
    swappingOrientation = false
}

如果您正在交换方向或不在swappingOrientation变量中,您将持有信息。在创建片段的开始时,它会改变方向,只有当没有保存状态时。只有在当前未更改方向时,才会再次更改方向。

这是一个超级快速的解决方案,当您返回上一个片段时,它会产生屏幕闪烁。我也没有完全测试它,所以它可能有其他问题,所以请记住这一点。

于 2019-02-11T16:40:36.320 回答
2

第一个片段:

@Override
    public void onResume() {
        super.onResume();
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    }

第二个片段:

     @Override
        public void onResume() {
            super.onResume();
 getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
   }
于 2019-09-17T16:28:54.000 回答
1

这是一个老问题,但回答这个问题只是因为有人想要 OP 需要的解决方案。实现这一点的简单方法如下。

public class LandscapeFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

       getActivity().setRequestedOrientation(
              ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

   @Override
   public View onDestroyView() {
       super.onDestroyView();

       getActivity().setRequestedOrientation(
             ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

然后从您想要以横向模式启动它的任何片段中extendLandscapeFragment

于 2019-10-01T17:12:01.063 回答
0

好吧,在我差点把我的头弄晕之后,这对我有用的喷气背包导航组件碎片

所以这是基本片段类

abstract class BaseFragment : Fragment(){   

var rotated = false

fun rotate() {
        val currentOrientation = activity?.resources?.configuration?.orientation //this is diffrent from  Configuration.ORIENTATION_LANDSCAPE
        Log.e("currentOrientation--->",currentOrientation.toString())

        if (currentOrientation != null) {
            if (currentOrientation !=  Configuration.ORIENTATION_LANDSCAPE) {
                activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            }else{
                rotated=true
            }
        }else{
            //impossible
            activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }
    }


    override fun onDestroyView() {
        super.onDestroyView()
        if (rotated)
            activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
    }

}

我把它放在我希望它被美化的任何片段中

override fun onStart() {
    super.onStart()
    rotate()
}
于 2020-01-01T12:06:49.007 回答
-1

在您的片段 FragA 中:

@Override
public void onResume() {
    super.onResume();
    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}


@Override
public void onPause() {
    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    super.onPause();
}

onPause方法代码确保您的活动回到纵向

于 2018-09-17T05:42:00.433 回答