我最终做的是调整布局以处理带有空容器的横向和纵向,这些容器稍后将填充适当的片段:
<RelativeLayout>
...
<FrameLayout
android:id="@+id/xmpRightContainer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true" >
</FrameLayout>
<FrameLayout
android:id="@+id/xmpBottomContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</FrameLayout>
</RelativeLayout>
在 onConfigurationChanged 中:
if (xmpFrag != null)
{
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.remove(getSupportFragmentManager().findFragmentByTag(XmpFragment.FRAGMENT_TAG));
ft.commit();
fm.executePendingTransactions();
}
xmpFrag = new XmpFragment();
int container;
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
if (isPortrait)
{
container = R.id.xmpBottomContainer;
}
else
{
container = R.id.xmpRightContainer;
}
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(container, xmpFrag, XmpFragment.FRAGMENT_TAG);
ft.commit();
fm.executePendingTransactions();
这完美地工作并且现在旋转设备对用户来说是无缝的。