【问题标题】:Android Status Bar transparent when using AppTheme.NoActionBar使用 AppTheme.NoActionBar 时 Android 状态栏透明
【发布时间】:2016-10-03 15:30:45
【问题描述】:

当我通过android:theme 将自动生成的AppTheme.NoActionBar 应用到我的活动时,如下所示:

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>

我的 MainActivity 在顶部呈现透明状态栏,最终有一个白色背景和白色文本。如果设备正在充电,这是唯一可见的符号。

来自 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>

在第二种样式中,您可以看到AppTheme.NoActionBar,它默认继承了 AppTheme,但是在这两种样式中都没有指定状态栏应该是透明的。

【问题讨论】:

    标签: android android-actionbar themes auto-generate


    【解决方案1】:

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

    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 在问题中提供的 values/styles.xml 中定义,该文件仍然有效,但该文件仅用于 API 21 下的设备.如果您查看代码,只需在最后看到“透明”一词即可立即识别问题。向左一点,我们看到statusBarColor,瞧。这就是问题所在。如果您不希望为更多最新用户提供此样式,您可以简单地删除该行样式。

    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>
    

    这是删除样式的结果:

    不错的不透明状态栏

    【讨论】:

    • 他们为什么这样做?
    • @jsHate 我相信这只是为了在不同的世代中引入不同的样式。
    • 不敢相信我浪费了大量时间试图找出问题所在。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 2017-07-07
    相关资源
    最近更新 更多