我写了一个应用程序,需要竖屏和横屏显示不同的布局
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setNormalHorizontalView();
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setNormalVerticalView();
}在竖屏时会使用ActivityManager进行视图切换
mFrameLayout.removeAllViews();
switch(mPresentActivity){
case 0:
mFrameLayout.addView((mLocalActivityManager.startActivity("", new Intent(GinwaveIMusic.this,GinwavePlayer.class)).getDecorView()));
break;
case 1:
mFrameLayout.addView((mLocalActivityManager.startActivity("", new Intent(GinwaveIMusic.this,Album.class)).getDecorView()));
break;
case 2:
mFrameLayout.addView((mLocalActivityManager.startActivity("", new Intent(GinwaveIMusic.this,GinwaveMusic.class)).getDecorView()));
break;
case 3:
mFrameLayout.addView((mLocalActivityManager.startActivity("", new Intent(GinwaveIMusic.this,PlayList.class)).getDecorView()));
break;
}从竖屏切换到横屏然后再切换到竖屏时会出现The specified child already has a parent. You must call removeView() on the child's parent first.的bug,我按照提示已经添加了mFameLayout.removeAllViews()函数,但还是会出现这个问题,我想可能是由于在竖屏时已经执行过mLocalActivityManager.startActivity("", new Intent(GinwaveIMusic.this,GinwavePlayer.class)).getDecorView()方法,从横屏转换到竖屏后又会执行这个方法,而第一次切换后并没有进行对这个View进行释放,所以我在由竖屏切换到横屏时对这个View进行释放,我在onConfiguration中增加了以下代码后就没有问题了
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
if(mFrameLayout != null){
mFrameLayout.removeAllViews();
}
setNormalHorizontalView();
}
解决屏幕旋转时View重复添加的问题
本文介绍了一种在Android应用中处理不同屏幕方向(竖屏和横屏)下布局变化的方法,并详细解释了如何避免在多次屏幕旋转时出现View重复添加导致的错误。
465

被折叠的 条评论
为什么被折叠?



