我有一个FragmentActivity,里面有一些片段。当我从片段(片段A)开始,我将手机的纵向位置移动到下一个片段(片段B),我在代码中设置了setOrientation()to Landscape我拿到黑色屏幕时在我将手机转为横向之前,手机的纵向和布局将不会显示在第二个片段上。我的印象是,无论你怎么拿着手机,当你做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);
}
}
弗拉加:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Set the orientation to be in portrait and landscape.
((MainActivity) getActivity()).setOrientation(1);
...
}
FragB:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Set the orientation to be in landscape.
((MainActivity) getActivity()).setOrientation(5);
...
}
答案 0 :(得分:1)
您必须在onActivityCreated中调用getActivity()