简体   繁体   English

获取当前方向并锁定

[英]Get current orientation and lock

I'm trying to get the current orientation of an activity and lock it like this but it just goes to portrait even when opened in landscape mode. 我正在尝试获取活动的当前方向并将其锁定为这种状态,但即使在横向模式下打开时,它也只能变为纵向。

//getRequestedOrientation();
setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_LOCKED);

Thanks 谢谢

You have screen orientation locked, it will show your app in portrait mode, you should use screen orientation sensor. 您已锁定屏幕方向,它将以纵向模式显示您的应用,您应该使用屏幕方向传感器。

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_SENSOR);   

Edit. 编辑。

int str = 0;
    try {
            str = Settings.System.getInt(contexto.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION);
        } catch (SettingNotFoundException e) {          
                e.printStackTrace();
        } 
    if(str!=0) 
    {
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_SENSOR);   
    }

Also check this link, I think it can help you. 同时检查链接,我认为它可以为您提供帮助。

This seems to do the trick: 这似乎可以解决问题:

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM