Android实现动态改变屏幕方向(Landscape & Portrait)

时间:2023-02-08 10:13:44

1.AndroidManifest.xml:

<activity>
  android:screenOrientation="portrait"

  。。。

2.xx.java:

<activity>
android:screenOrientation="portrait"
//如果是竖排,则改为横排;反之然
if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}