古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述Android 设定横屏,禁止屏幕旋转,Activity重置 [更新视频播放器相关]相关的知识,希望能为你提供帮助。
1. 设定屏幕方向
当指定了屏幕的方向后(非SCREEN_ORIENTATION_UNSPECIFIED),屏幕就不会自己主动的旋转了
有2中方式控制屏幕方向:
1.1 改动androidManifest.xml
在AndroidManifest.xml的activity中增加:横屏:
android:screenOrientation=”landscape”竖屏:
android:screenOrientation=”portrait”
1.2 setRequestedOrientation横屏:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
【Android 设定横屏,禁止屏幕旋转,Activity重置 [更新视频播放器相关]】竖屏:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ActivityInfo:
int |
SCREEN_ORIENTATION_BEHIND |
Constant
corresponding
to
behind
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_FULL_SENSOR |
Constant
corresponding
to
fullSensor
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_FULL_USER |
Constant
corresponding
to
fullUser
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_LANDSCAPE |
Constant
corresponding
to
landscape
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_LOCKED |
Constant
corresponding
to
locked
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_NOSENSOR |
Constant
corresponding
to
nosensor
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_PORTRAIT |
Constant
corresponding
to
portrait
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_REVERSE_LANDSCAPE |
Constant
corresponding
to
reverseLandscape
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_REVERSE_PORTRAIT |
Constant
corresponding
to
reversePortrait
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_SENSOR |
Constant
corresponding
to
sensor
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_SENSOR_LANDSCAPE |
Constant
corresponding
to
sensorLandscape
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_SENSOR_PORTRAIT |
Constant
corresponding
to
sensorPortrait
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_UNSPECIFIED |
Constant
corresponding
to
unspecified
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_USER |
Constant
corresponding
to
user
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_USER_LANDSCAPE |
Constant
corresponding
to
userLandscape
in
the
screenOrientation
attribute. |
int |
SCREEN_ORIENTATION_USER_PORTRAIT |
Constant
corresponding
to
userPortrait
in
the
screenOrientation
attribute. |
2. 禁止 屏幕旋转后重置Activity
屏幕旋转后会强制调用Activity.onCreate方法,所以会重置Activity
禁止方法:
改动AndroidManifest.xml
android:configChanges=”orientation”
近期在做Android的播放器。遇到採用以上方法后,仍然出现屏幕旋转后触发Activity.onCreate,经查阅资料后,发现须要加入screenSize处理
详细代码为:
android:configChanges=" orientation|keyboardHidden|screenSize"
禁止重置Activity会造成Screen的宽高颠倒, 须要手动修正。
推荐阅读
- Android推断是否有sd卡
- 格式化磁盘,提示 is apparently in use by the system
- android 5.X Toolbar+DrawerLayout实现抽屉菜单
- Android---58---初学GPS定位
- applicationContext.xml(Spring)
- 如何在CentOS上安装Eclipse
- 如何在CentOS上安装Apache CouchDB
- 如何在CentOS上安装Composer
- 如何在CentOS上安装BlueFish。