我正在尝试将屏幕方向锁定到调用应用程序时的位置,以免在屏幕旋转时崩溃(是的,我使用过
android:configChanges="方向|keyboardHidden"
并且它在低 Android 版本上运行良好,但似乎对于更高版本应该是
android:configChanges="方向|keyboardHidden|screenSize"
并且较低的android版本不支持screenSize参数)。
所以,我试过这个解决方案(1):
setRequestedOrientation (getResources().getConfiguration().orientation);
还有另一个(2):
setRequestedOrientation ( getScreenOrientation() );
在哪里,
public int getScreenOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = Configuration.ORIENTATION_UNDEFINED;
if(getOrient.getWidth() < getOrient.getHeight()){
orientation = Configuration.ORIENTATION_PORTRAIT;
}else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
return orientation;
}
并且,在两种情况下(1 和 2),结果都是:当我以纵向启动应用程序时,它工作正常。屏幕方向锁定为纵向模式。但是,当我在横向进行时,它会不断改变方向。
我做错了什么?提前致谢