Android 设置横竖屏,禁止屏幕旋转,禁止旋转后Activity重置

在没有设置禁止手机旋转时,Android App在会随着手机的旋转切换横\竖屏。但很多时候我们希望App 旋转,以下归纳集中方法:

1、AndroidManifest.xml文件中设置Activity属性

横屏:android:screenOrientation=”landscape”
竖屏:android:screenOrientation=”portrait”

2、Activity代码文件中设置

横屏:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
竖屏:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

另外在允许屏幕旋转的时候,App会重置Activity的onCreate,导致一些不必要的错误。下面方法可以禁止Activity重置。

AndroidManifest.xml中,Activity添加属性android:configChanges=”orientation”

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s