使用AppTheme.NoActionBar时,Android状态栏是透明的

Sup*_*Ham 14 android themes auto-generate android-actionbar

当我AppTheme.NoActionBar通过android:theme这样的方式将自动生成应用于我的活动时:

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>
Run Code Online (Sandbox Code Playgroud)

我的MainActivity通过顶部的透明状态栏进行渲染,最终具有白色背景和白色文本.如果设备正在充电,那么这是唯一可见的符号.

透明的ActionBar

来自AppTheme.NoActionBar的透明ActionBar

这是我的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>
Run Code Online (Sandbox Code Playgroud)

在第二种样式中,您可以看到AppTheme.NoActionBar,默认情况下会继承AppTheme,但是在任何一种样式中都没有指定状态栏应该是透明的.

Sup*_*Ham 42

如果您遇到此问题,可能会有一个名为values-v21的文件夹,其中包含一个名为styles.xml的文件.此文件为使用API 21+(Android 5.0+)的设备定义样式.此文件也很可能包含以下命名的样式AppTheme.NoActionBar.听起来有点熟?

值-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>
Run Code Online (Sandbox Code Playgroud)

AppTheme.NoActionBar在问题提供的values/styles.xml中定义,它仍然有效,但该文件仅用于API 21下的设备.如果您查看代码,只需在最后看到透明一词即可立即识别问题.稍微向左,我们看到了statusBarColor瞧.这就是问题所在.如果您没有为更多最新用户提供此样式,您可以简单地删除该样式线.

值-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>
Run Code Online (Sandbox Code Playgroud)

这是删除样式的结果:

修正了结果

好的'不透明状态栏