简体   繁体   English

在片段中将方向锁定为横向

[英]Lock the orientation to landscape in a fragment

My app has navigation set up and it is a single activity app.我的应用程序设置了导航,它是一个单一的活动应用程序。 Fragments are linked like this:片段链接如下:

MainFragment -> GameFragment MainFragment -> GameFragment

MainFragment -> GameSetupFragment -> GameFragment MainFragment -> GameSetupFragment -> GameFragment

What I need to accomplish: Depending on settings, GameFragment sometimes should be locked in the landscape mode.我需要完成的工作:根据设置,GameFragment 有时应该锁定在横向模式下。 When I did this in an activity, I wrote a line of code:当我在一个活动中这样做时,我写了一行代码:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

What can I do if I use fragments?如果我使用碎片,我该怎么办? Is there a way to tell the fragment owner (ie the activity) to switch the orientation?有没有办法告诉片段所有者(即活动)切换方向?

假设您知道您的片段已附加到活动,您可以在片段中调用requireActivity()来获取您的活动:

requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM