# 應用程序方向（橫向或縱向）如何鎖定？

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


## 最佳答案

First, don't use setRequestedOrientation() if you can avoid it. Use the android:screenOrientation attribute in your manifest element instead.

Second, you will also need android:configChanges="keyboardHidden|orientation" in your manifest element to prevent the destroy/recreate cycle.