简体   繁体   中英

Android dark theme doesn't set windowBackground

I have a theme that works but refuses to set android:windowBackgorund and android:colorBackground. The rest of the code works properly, and if I switch the items in the default theme it works properly.

styles.xml:

    <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:navigationBarColor">@color/colorPrimary</item>
    <item name="android:radioButtonStyle">@style/radioLight</item>
</style>

<style name="Dark" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:navigationBarColor">@color/colorPrimary</item>
    <item name="android:windowBackground">@color/grey</item>
    <item name="android:colorBackground">@color/grey</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">@color/soft_grey</item>
    <item name="android:radioButtonStyle">@style/radioDark</item>
</style>

Main:

setTheme(R.style.Dark);

Don't know if it's useless activity_main xml:

    <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:scrollbars="none"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="35dp" />

</android.support.design.widget.AppBarLayout>

What's the shameful mistake i committed?

android:windowBackground only accepts a drawable. Try changing it to something like

<item name="android:windowBackground">@drawable/abc</item>

And create a file abc.xml such as

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/grey"/>
</shape>

And just checking, do you call setTheme before calling super.onCreate() ?

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