Change screen orientation in Android without reloading the activity

前端 未结 5 729
梦谈多话
梦谈多话 2020-12-16 10:40

I want to change the orientation programmatically while running my Android App, with these lines of code:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTA         


        
相关标签:
5条回答
  • 2020-12-16 10:50

    Call this method And Set manifest file

    android:configChanges="orientation|screenSize|keyboardHidden"
    
    public void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
    
            // Checks the orientation of the screen
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
            } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
                Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
            }
        }
    
    0 讨论(0)
  • 2020-12-16 10:52

    See the edit by @luisfer. For targeting Android 3.2 and above, you need BOTH

    android:configChanges="orientation|screenSize"
    

    http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

    0 讨论(0)
  • 2020-12-16 10:53

    You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter

    0 讨论(0)
  • 2020-12-16 11:01

    Ansewered here:

    Android, how to not destroy the activity when I rotate the device?

    Add:

    android:configChanges="orientation"

    To your androidmanifest.

    see:

    http://developer.android.com/guide/topics/manifest/activity-element.html#config

    0 讨论(0)
  • 2020-12-16 11:05

    In AndroidManifest file add android:configChanges="orientation" for the activity you want to handle this orientation

    In activity use onConfigurationChange overrided method. Do task you want to handle in orientation change.

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