Android: listen for Orientation change?

前端 未结 3 2075
悲哀的现实
悲哀的现实 2020-12-08 10:12

How would I listen for orientation change in Android? and do certain things when the user has switched to landscape?

3条回答
  •  遥遥无期
    2020-12-08 10:55

    You have a couple of choices:

    • Use an OrientationEventListener, which has a method called onOrientationChanged.

    • Use config changes:

    In your Manifest, put:

    
    

    And, in your Activity, override onConfigurationChanged:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        int newOrientation = newConfig.orientation;
    
        if (newOrientation == Configuration.ORIENTATION_LANDSCAPE) {
          // Do certain things when the user has switched to landscape.    
        }   
    }
    

    Here is a good tutorial about it.

提交回复
热议问题