使用AppTheme.NoActionBar

时间:2016-06-03 14:05:18

标签: android android-actionbar themes auto-generate

当我通过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>

我的 MainActivity 会在顶部显示一个透明的状态栏,最终会显示一个白色背景,并在其上方显示白色文字。如果设备正在充电,那么这是唯一可以看到的符号。

Transparent 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>

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

1 个答案:

答案 0 :(得分:38)

如果您遇到此问题,可能会有一个名为 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>

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>

以下是该样式删除的结果:

Fixed result

Good ol&#39;不透明状态栏