简体   繁体   中英

Android Status Bar transparent when using AppTheme.NoActionBar

When I apply the auto-generated AppTheme.NoActionBar to my activity via android:theme like so:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="mypackage">

    <application
        ...
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar" />

    </application>

</manifest>

My MainActivity is rendered with a transparent status bar at the top, which eventually has a white background, and white text over it. If the device is charging, that is the only symbol to be seen.

Transparent ActionBar

Transparent ActionBar from AppTheme.NoActionBar

Here is my values/styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>

In the second style, you can see AppTheme.NoActionBar , which by default inherits AppTheme, however nowhere in either style specifies that the status bar should be transparent.

Chances are, if you've come across this issue, you have a folder called values-v21 and in it a file called styles.xml . This file defines styling for devices using API 21+ ( Android 5.0+ ). This file will also most likely contain the following style named AppTheme.NoActionBar . Sound familiar?

values-v21/styles.xml:

<resources>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>

AppTheme.NoActionBar was defined in the values/styles.xml provided in the question, which is still valid, however that file is only used for devices under API 21 . If you look at the code, you can immediately identify the issue just by seeing the word transparent towards the very end. A little to the left and we see statusBarColor and voila. That's the issue. If you do not which to provide this styling for more up-to-date users you can simply remove that line of styling.

values-v21/styles.xml:

<resources>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    </style>
</resources>

And here's the result of that style removal:

修正了结果

Good ol' opaque status bar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM