Usually when writing a new demo, it takes a long time to set up the immersive effect, because many of the online claims are not completely correct, and eventually it is abandoned and left to do other features. Now record a set of correct setting methods, convenient after the reference.

Step 1: Add the following line to the Activity’s onCreate method

supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
Copy the code

This line of code should be in super.onCreate(savedInstanceState); Call before, otherwise an error will be reported

Step 2: Customize an immersive theme

<! -- values -->
<style name="ImmersiveTheme" parent="(Global topic name used by application)">
    <item name="windowNoTitle">true</item>
</style>

<! -- values-v19 -->
<style name="ImmersiveTheme" parent="(Global topic name used by application)">
    <! The following two properties can only be set with Android 4.4 or later -->
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="windowNoTitle">true</item>
</style>
    
<! -- values-v21 -->
<style name="ImmersiveTheme" parent="(Global topic name used by application)">
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <! -- The following attribute can only be set after Android 5.0 -->
    <! -- Starting with Android 5.0, you need to set the color to transparent, otherwise the navigation bar will show the system's default light gray -->
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="windowNoTitle">true</item>
</style>
Copy the code

Step 3: the Activity of XML file set android: fitsSystemWindows = “true”

<LinearLayout
    .
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        .
        android:fitsSystemWindows="true">
        <Toolbar
            .
            android:layout_width="match_parent"
            android:layout_height="? attr/actionBarSize">
            <TextView
                .
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>.</Toolbar>.</LinearLayout>.</LinearLayout>
Copy the code

As shown above, all will be in contact with the status bar at the top of the control using a Layout packaged and give the Layout setting properties android: fitsSystemWindows = “true”. Be careful not to set this property for the root layout.

(if the XML file used by the include tag, the attribute android: fitsSystemWindows = “true” needs to be set in the include references on the root of the XML file layout, do not set on the include tag)

Step 4: Set up an immersive theme in Androidmanifest.xml

<! -- Global Settings -->
<application
        .
        android:theme="@style/ImmersiveTheme">.</application>

<! -- Activity setting -->
<activity
    .
    android:theme="@style/ImmersiveTheme">.</activity>
Copy the code

conclusion

After this setting, the background color of the status bar changes with the background color of the Activity’s root layout. The status bar text color is white by default.