How to Disable landscape mode in Android?

前端 未结 30 3028
Happy的楠姐
Happy的楠姐 2020-11-22 13:45

How can I disable landscape mode for some of the views in my Android app?

30条回答
  •  旧时难觅i
    2020-11-22 14:09

    if your activity related to the first device orientation state,get the current device orientation in the onCreate method and then fix it forever:

            int deviceRotation = ((WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
    
            if(deviceRotation == Surface.ROTATION_0) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
            else if(deviceRotation == Surface.ROTATION_180)
            {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            }
            else if(deviceRotation == Surface.ROTATION_90)
            {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
            else if(deviceRotation == Surface.ROTATION_270)
            {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            }
    

提交回复
热议问题