- 通过 android.app.Activity.getRequestedOrientation()
<activity android:name=".MyActivity" android:screenOrientation="landscape" android:label="@string/app_name"/>
int orientation = getRequestedOrientation(); if (orientation == Configuration.ORIENTATION_LANDSCAPE) { // 横屏 } else if (orientation == Configuration.ORIENTATION_PORTRAIT) { // 竖屏 }
- 通过 android.view.Display.getOrientation() ( This method is deprecated. use getRotation() )
int orientation = getWindowManager().getDefaultDisplay().getRotation(); if (orientation == Configuration.ORIENTATION_LANDSCAPE) { // 横屏 } else if (orientation == Configuration.ORIENTATION_PORTRAIT) { // 竖屏 }
- 通过android.view.Display.getRotation()
int rotation = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)) .getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: // Test Log.i("Activity", "ROTATION_0 "); break; case Surface.ROTATION_90: // Test Log.i("Activity", "ROTATION_90 "); break; case Surface.ROTATION_180: // Test Log.i("Activity", "ROTATION_180 "); break; case Surface.ROTATION_270: // Test Log.i("Activity", "ROTATION_270 "); break; }
- 通过在Manifest中指定Activity的android:ConfigChanges="orientation"属性, 重写android.app.Activity.onConfigurationChanged(Configuration newConfig),当Configuration 更改时回调该方法。
如果在 manifest中为activity设置了此属性,则在屏幕旋转的时候不会再次调用onCreate方法。
时间有限,如有错误,感谢指出
<activity
android:name=".MyActivity"
android:configChanges="orientation"
android:label="@string/app_name"/>
@Override public void onConfigurationChanged(Configuration newConfig) { // 此回调方法只有在横竖屏切换的时候才回触发,如果一次旋转180度,不会触发 super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // 横屏 } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ // 竖屏 } }