Android 全屏沉浸模式(支援API 19及以上系統)
阿新 • • 發佈:2019-01-24
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>