Android 在我的活动中仅为 1 个片段设置方向,而其余片段为纵向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12704009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the orientation for only 1 fragment in my activity while the rest is in portrait
提问by John Ubonty
My app needs to be in portrait mode so I set it in the manifest by:
我的应用程序需要处于纵向模式,因此我通过以下方式在清单中设置它:
android:screenOrientation="portrait"
But I just recently added another fragment (FragA) that just looks and functions 10x better in landscape. Is there something I can put inside of my FragA to just make that fragment in landscape while retaining the rest of the app in portrait or by doing this will I have to add something to my other fragments to keep them retained as portrait?
但我最近添加了另一个片段 (FragA),它在横向上的外观和功能都提高了 10 倍。有什么我可以放在我的 FragA 中的东西,只是让该片段处于横向状态,同时将应用程序的其余部分保留为纵向,或者通过这样做,我是否必须向我的其他片段添加一些内容以将它们保留为纵向?
采纳答案by Dinesh Venkata
Orientation attribute is per activity so you can declare the orientation for only the activity that contains the fragment so that it is in landscape and the rest of the activities will remain as they are.
方向属性是针对每个活动的,因此您可以仅声明包含片段的活动的方向,使其处于横向状态,而其余活动将保持原样。
回答by pradeep.k
Use the following code line in the fragment where you want a specific (in this case portrait) orientation.
在您想要特定(在本例中为纵向)方向的片段中使用以下代码行。
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
If you want to have a orientation in a fragment, that is based on the way the user holds his device, then use the following code line.
如果您想在片段中设置方向,即基于用户握持设备的方式,请使用以下代码行。
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Hope, this will give you the intended solution.
希望,这将为您提供预期的解决方案。
回答by Philip BH
In each of your fragments, set the requested orientation.
在您的每个片段中,设置请求的方向。
Reference doc: http://developer.android.com/reference/android/content/pm/ActivityInfo.html
参考文档: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);
}
回答by David Sucharda
So I'm dealing with this issue now. We have only portraitmode application (for now). But there is one fragment that needs to be in landscape. We are using single Activity approach so the accepted answer will not help me.
所以我现在正在处理这个问题。我们只有纵向模式应用程序(目前)。但是有一个片段需要在Landscape 中。我们正在使用单一活动方法,因此接受的答案对我没有帮助。
The fastest solution I could think of is this one.
我能想到的最快的解决方案是这个。
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
}
You hold the information if you are swapping orientation or not in swappingOrientationvariable. At the beggining when the fragment is created it will change orientation, only when there is no saved state. And orientation is changed back again only when it is not being currently changed.
如果您正在交换方向或不在swappingOrientation变量中,则您保存信息。在开始创建片段时,它会改变方向,只有当没有保存状态时。并且只有在当前未更改时才会再次更改方向。
This is a super quick solution and it can produce screen blinking when you return to previous fragment. I also did not fully test it so it can have other issues, so keep that in mind.
这是一个超级快速的解决方案,当您返回到上一个片段时,它会导致屏幕闪烁。我也没有完全测试它,所以它可能有其他问题,所以请记住这一点。
回答by Mohamed Ben Romdhane
First Fragment:
第一段:
@Override
public void onResume() {
super.onResume();
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Second Fragment:
第二个片段:
@Override
public void onResume() {
super.onResume();
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
回答by Vinayak Bevinakatti
This is old question but answering this just incase anyone wanted the solution as the OP needed. The simple way to achieve this is as follows.
这是一个老问题,但回答这个只是为了以防万一有人想要 OP 需要的解决方案。实现此目的的简单方法如下。
public class LandscapeFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
@Override
public View onDestroyView() {
super.onDestroyView();
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Then extend
this LandscapeFragment
from any of your fragment.
然后extend
这LandscapeFragment
来自您的任何片段。
回答by Ahmed D. Sherif
ok after i almost blew my head off this worked for me with jetpack navigation components fragments
好吧,在我差点把头吹掉之后,这对我有用的喷气背包导航组件碎片
so this is the base fragment class
所以这是基本片段类
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
}
}
and i put this in any fragment i wish it to be landscaped
我把它放在任何我希望它被美化的片段中
override fun onStart() {
super.onStart()
rotate()
}
回答by Cristian Chávez
In your fragment FragA :
在您的片段 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();
}
onPausemethod code ensure your activity back to portrait orientation
onPause方法代码确保您的活动回到纵向