3

我有一个 FragmentActivity,里面有一些片段。当我从我所在的片段(片段 A)(我将手机处于纵向位置)到我在代码 setOrientation() 中设置的下一个片段(片段 B)到横向时,当我持有时出现黑屏在我将手机转到横向之前,纵向的手机和布局不会显示在第二个片段上。我的印象是,无论您如何握住手机,当您执行 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);
    }
}

片段A:

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

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

片段B:

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

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

1 回答 1

1

您必须调用getActivity()onActivityCreated!

于 2013-08-01T23:45:57.193 回答