Window window = getWindow();
int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
requestWindowFeature(Window.FEATURE_NO_TITLE); //隐藏状态栏
window.setFlags(flag, flag); //全屏显示
注意:必须在setContentView之前执行,否则会报错。另外这种全屏显示方式的缺点是,会有隐藏标题栏和隐藏状态栏的动画过渡效果。
Theme控制res > values > themes.xml
<style name="AppTheme.ZGSBSPlayer.FullScreen" parent="Theme.AppCompat.NoActionBar">
- "windowNoTitle"
>true
- "android:windowFullscreen"
>true
style>
AndroidManifest.xml
<activity android:name=".vrplayer.VRPlayerActivity"
android:theme="@style/AppTheme.ZGSBSPlayer.FullScreen">
可能会报错 You need to use a Theme.AppCompat theme (or descendant) with this activity
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
默认当用户手机重力感应器打开后,屏幕旋转时,Activity的生命周期会重新加载onDestroy-> onCreate即当前Activity被销毁了。
想要很好的支持屏幕旋转,建议在res中建立layout-land和layout-port两个文件夹,分别设定布局。
设置AndroidManifest.xml对应的 activity 属性
默认的情况下,应用启动后,会固定为指定的屏幕方向,即使屏幕旋转,Activity也不会出现销毁或者转向等任何反应。
android:screenOrientation="landscape" //横屏
android:screenOrientation="portrait" //竖屏
@Override
protected void onResume() {
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
super.onResume();
}
如果手机关闭了重力感应器,或者同上activity中固定了屏幕方向,则默认情况下该Activity不会响应屏幕旋转事件。
如果在此情况下依然希望Activity响应屏幕旋转,则在onCreate()方法中添加代码
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
AndroidManifest.xml
# 在相应的Activity声明中添加属性
android:configChanges="orientation|keyboard|keyboardHidden|screenSize|layoutDirection"
以前只需要添加orientation后续需多加一个screenSize再后来需要添加上layoutDirection否则onConfigurationChanged不会被调用
如果缺少了keyboardHidden选项,不能防止Activity的销毁,并且在之后提到的onConfigurationChanged事件中只能捕获到坚屏变横屏的事件,不能捕获横屏变坚屏。