1. 程式人生 > >沉浸式狀態列實現,讓狀態列和導航欄變色吧

沉浸式狀態列實現,讓狀態列和導航欄變色吧

該功能只能適應Android4.4及以上版本,4.4版本真機,5.0版本真機,6.0版本模擬器測試通過

該方法來自https://github.com/jgilfelt/SystemBarTint開源庫

效果圖:


實現步驟:

(一)Android Studio專案新增依賴包

compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

(二)在要實現的activity中新增一下程式碼
<span style="white-space:pre">	</span>SystemBarTintManager manager=new SystemBarTintManager(this);
        /**
         * 這兩句程式碼如同:
         * if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
         *       getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
         *       getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
         *  }
         */
        manager.setNavigationBarTintEnabled(true);
        manager.setStatusBarTintEnabled(true);
        //修改狀態列顏色
        manager.setStatusBarTintColor(Color.parseColor("#552233"));
        //修改導航欄顏色
        manager.setNavigationBarTintColor(Color.parseColor("#552233"));
(三)在activity中相應的佈局檔案根佈局中新增如下程式碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"//必填
    android:clipToPadding="false"//
    android:orientation="vertical">
(四)修改相關activity主題風格

values下的styles.xml檔案

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    
    <style name="FullBleedTheme" parent="android:Theme.Light.NoTitleBar">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    
    <style name="ActionBarTheme" parent="android:Theme.Light">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>
values-v14下的styles.xml檔案
<resources>

    <!--
        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Holo">
        <!-- API 14 theme customizations can go here. -->
    </style>
    
    <style name="FullBleedTheme" parent="android:Theme.Holo.Light.NoActionBar">
        <!-- API 14 theme customizations can go here. -->
    </style>
    
    <style name="ActionBarTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
        <item name="android:actionBarStyle">@style/ActionBarStyle</item>
    </style>
    
    <style name="ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/colorPrimary</item>
    </style>

</resources>
values-v19下的styles.xml檔案
<resources>

    <!--
        Base application theme for API 19+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 19+ devices.
    -->    
    <style name="FullBleedTheme" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor">
        <!-- API 19 theme customizations can go here. -->
    </style>

</resources>
在AndroidManifest.xml中修改activity的主題
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.qdq.statusbarornavigationbartint">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">//theme屬性修改主題樣式
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>