How do I disable orientation change on Android?

后端 未结 12 969
一向
一向 2020-11-22 02:45

I have an application that I just would like to use in portrait mode, so I have defined android:screenOrientation=\"portrait\" in the manifest XML. This works OK for the HTC

相关标签:
12条回答
  • 2020-11-22 03:01

    I've always found you need both

    android:screenOrientation="nosensor" android:configChanges="keyboardHidden|orientation"
    
    0 讨论(0)
  • 2020-11-22 03:02

    Add

    android:configChanges="keyboardHidden|orientation|screenSize" 
    

    to your manifest.

    0 讨论(0)
  • 2020-11-22 03:05

    Use this..

        android:screenOrientation="portrait"
    
    0 讨论(0)
  • 2020-11-22 03:08

    Update April 2013: Don't do this. It wasn't a good idea in 2009 when I first answered the question and it really isn't a good idea now. See this answer by hackbod for reasons:

    Avoid reloading activity with asynctask on orientation change in android

    Add android:configChanges="keyboardHidden|orientation" to your AndroidManifest.xml. This tells the system what configuration changes you are going to handle yourself - in this case by doing nothing.

    <activity android:name="MainActivity"
         android:screenOrientation="portrait"
         android:configChanges="keyboardHidden|orientation">
    

    See Developer reference configChanges for more details.

    However, your application can be interrupted at any time, e.g. by a phone call, so you really should add code to save the state of your application when it is paused.

    Update: As of Android 3.2, you also need to add "screenSize":

    <activity
        android:name="MainActivity"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation|screenSize">
    

    From Developer guide Handling the Configuration Change Yourself

    Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must declare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

    0 讨论(0)
  • 2020-11-22 03:09

    To lock the screen by code you have to use the actual rotation of the screen (0, 90, 180, 270) and you have to know the natural position of it, in a smartphone the natural position will be portrait and in a tablet, it will be landscape.

    Here's the code (lock and unlock methods), it has been tested in some devices (smartphones and tablets) and it works great.

    public static void lockScreenOrientation(Activity activity)
    {   
        WindowManager windowManager =  (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);   
        Configuration configuration = activity.getResources().getConfiguration();   
        int rotation = windowManager.getDefaultDisplay().getRotation(); 
    
        // Search for the natural position of the device    
        if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE &&  
           (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) ||  
           configuration.orientation == Configuration.ORIENTATION_PORTRAIT &&   
           (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270))   
        {   
            // Natural position is Landscape    
            switch (rotation)   
            {   
                case Surface.ROTATION_0:    
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);    
                    break;      
                case Surface.ROTATION_90:   
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
                break;      
                case Surface.ROTATION_180: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
                    break;          
                case Surface.ROTATION_270: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
                    break;
            }
        }
        else
        {
            // Natural position is Portrait
            switch (rotation) 
            {
                case Surface.ROTATION_0: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
                break;   
                case Surface.ROTATION_90: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
                break;   
                case Surface.ROTATION_180: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
                    break;          
                case Surface.ROTATION_270: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
                    break;
            }
        }
    }
    
    public static void unlockScreenOrientation(Activity activity)
    {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }
    
    0 讨论(0)
  • 2020-11-22 03:11

    In the AndroidManifest.xml file, for each activity you want to lock add the last screenOrientation line:

    android:label="@string/app_name"
    android:name=".Login"
    android:screenOrientation="portrait" >
    

    Or android:screenOrientation="landscape".

    0 讨论(0)
提交回复
热议问题