gpt4 book ai didi

android - 如何将屏幕旋转锁定到当前状态

转载 作者:行者123 更新时间:2023-11-30 03:44:25 24 4
gpt4 key购买 nike

我正在尝试将屏幕方向锁定到应用程序被调用时的位置,以免在屏幕旋转时崩溃(是的,我已经使用过

android:configChanges="orientation|keyboardHidden"

它在低版本的 Android 上运行良好,但似乎对于高版本来说应该是这样

android:configChanges="orientation|keyboardHidden|screenSize"

并且screenSize参数在android低版本不支持。

所以,我尝试了这个解决方案 (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),结果都是:当我以纵向启动应用程序时,它工作正常。屏幕方向锁定为纵向模式。但是,当我在横向模式下执行此操作时,它会不断改变方向。

我做错了什么?提前致谢

最佳答案

方向配置的问题是,即使应用程序启动时设备处于横向,应用程序仍将以纵向模式创建,然后在需要时切换到横向模式。一般来说,除非是游戏,否则你不应该强行调整方向。您应该修复该应用,使其不会因方向更改而崩溃。

关于android - 如何将屏幕旋转锁定到当前状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com