简体   繁体   中英

Orientation Lock

How can I prevent my app form rotating. I want it so if the user is in portrait mode, the app will always be in portrait mode, and if they start the app in landscape the app will always be in lanscape (until the app is closed and started again)

public static void lockOrientation(Activity a) {
    if (a.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {
        a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}

public static void unlockOrientation(Activity a) {
    a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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