Adnroid 4.4(API level 19)中引入为setSystemUiVisibility()引入了一个新标签SYSTEM_UI_FLAG_IMMERSIVE,它可以让应用进入真正的全屏模式。
而现在我实现的方式是通过Theme以及系统提供的控件来实现全屏沉浸模式。
1.设置主题(Theme)
a./res/values
<style name= "AppTheme.NoActionBar" >
<item name= "windowActionBar" > false</item>
<item name= "windowNoTitle" > true</item>
<!--是否显示系统启动应用时默认加载的默认页-->
<item name= "android:windowDisablePreview" > false</item>
<!-- Customize your theme here. -->
<!--主调色彩:主调色彩是用于应用品牌推广的色彩。作为action bar的背景色最近的任务title和其它边缘效果。-->
<item name= "colorPrimary" > @color/colorPrimary </item>
<!-- 主调的暗色: Darker作为主调色彩的加深,应用于状态栏 status bar.-->
<item name= "colorPrimaryDark" > @color/colorPrimaryDark </item>
<!-- 强调色彩:鲜明的扩展了主调色彩。应用于框架的控制。比如EditText,Switch-->
<item name= "colorAccent" > @color/colorAccent </item>
<!--窗口背景颜色-->
<item name= "android:windowBackground" > @color/window_bg</item>
<item name= "android:windowNoTitle" > true</item>
</style>
b. /res/values-v19
<style name="AppTheme.NoActionBar">
<item name= "windowActionBar" >false</item>
<item name= "windowNoTitle" >true</item>
<!--状态栏是否透明,API 19才出现的属性-->
<item name= "android:windowTranslucentStatus" >true</item>
</style>
c. /res/values-v21
<style name= "AppTheme.NoActionBar" >
<item name= "windowActionBar" > false</item>
<item name= "windowNoTitle" > true</item>
<!--状态栏是否透明,API 21才出现的属性-->
<item name= "android:windowDrawsSystemBarBackgrounds" > true</item>
<!--设置状态栏颜色,API 21才出现的属性-->
<item name= "android:statusBarColor" > @android:color/transparent </item>
</style>
2.设置fitsSystemWindows属性值,在5.0之后需要设置为true才能正常显示全屏沉浸式
a./res/values-v19
<bool name= "fitsSystemWindows" >false </bool>
b./res/values-v21
<bool name= "fitsSystemWindows" >true </bool>
c./res/values
<bool name= "fitsSystemWindows" >false </bool>
3.导入design Support包并创建UI XML文件
a.导入Design Support包
compile 'com.android.support:design:23.1.1'
b.UI创建XML文件
<?xml version="1.0"encoding="utf-8"?>
<android.support.design.internal.ScrimInsetsFrameLayout
xmlns: android="http://schemas.android.com/apk/res/android"
android :layout_width="match_parent"
android :layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id= "@+id/coordinator_layout"
android:layout_width= "match_parent"
android:layout_height= "0dp"
android:layout_weight= "1.0"
android :fitsSystemWindows= "@bool/fitsSystemWindows" >
</android.support.design.widget.CoordinatorLayout>
</android.support.design.internal.ScrimInsetsFrameLayout>