gpt4 book ai didi

java - 使用 setRequestedOrientation() 时出现黑屏

转载 作者:行者123 更新时间:2023-11-30 11:32:33 29 4
gpt4 key购买 nike

我有一个 FragmentActivity,里面有一些 fragment 。当我从手机处于纵向位置的 fragment ( fragment A)转到我在代码 setOrientation() 中设置为横向的下一个 fragment ( fragment B)时,我按住时出现黑屏在我将手机转到横向之前,纵向手机和布局不会显示在第二个 fragment 上。我的印象是,无论您如何握住手机,当您执行 setRequestedOrientation() 时,它都应该强制显示您提供的布局

视觉:

-----------         -----------         
| | | | ---------------
| | | | | |
| Frag A | -> | Frag B | -> Until I turn -> | Frag B |
| |(then to)| (black) | it to Landscape | (not black |
| | | | | anymore) |
| | | | ---------------
----------- -----------

代码:

保持方向变化的MainActivity:

    // Set Orientation of the Activity.
public void setOrientation(int sensor) {
if (sensor == 1) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
} else if (sensor == 2) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
} else if (sensor == 3) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else if (sensor == 4) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (sensor == 5) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}

fragment A:

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

// Set the orientation to be in portrait and landscape.
((MainActivity) getActivity()).setOrientation(1);
...
}

fragment B:

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

// Set the orientation to be in landscape.
((MainActivity) getActivity()).setOrientation(5);
...
}

最佳答案

您必须在 onActivityCreated 中调用 getActivity()!

关于java - 使用 setRequestedOrientation() 时出现黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16598342/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com