Android: listen for Orientation change?

前端 未结 3 2073
悲哀的现实
悲哀的现实 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条回答
  •  Happy的楠姐
    2020-12-08 10:54

    In your activity, you can override this method to listen to Orientation Change and implement your own code.

    public void onConfigurationChanged (Configuration newConfig)
    

    The Configuration Class has an int constant ORIENTATION_LANDSCAPE and ORIENTATION_PORTRAIT, there for you can check the newConfig like this:

    super.onConfigurationChanged(newConfig);

    int orientation=newConfig.orientation;
    
    switch(orientation) {
    
    case Configuration.ORIENTATION_LANDSCAPE:
    
    //to do something
     break;
    
    case Configuration.ORIENTATION_PORTRAIT:
    
    //to do something
     break;
    
    }
    

提交回复
热议问题