简体   繁体   中英

Setting Activities' Background Color

I am trying to change all of my activities' background color from white to black. I have tried everything - changing windowBackground, colorBackground, background but nothing seems to be working and I know my style is working because if I changed primary, accent colors they seem to be changing. The background tag only changes the action bar's color. Here's my code for the theme (what I have so far) -

<style name="Yoyo" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent1</item>
    <item name="android:windowBackground">@color/darkBackground</item>
    <item name="android:colorBackground">@color/darkBackground</item>
    <item name="android:textColor">#ffffff</item>
</style>

darkBackground is black - what I need for my app to show but its not showing.

Also, I have already tried using @drawable/my_drawable_file (in which I set black color) in windowBackground but even that doesn't work.

min sdk version - 21, target sdk version - 25

Try to change background your activity root element. For example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="your_color">

Try:

<color name="custom_theme_color">#000000</color>
<style name="CustomTheme"Parent="android:Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@color/custom_theme_color</item>
    <item name="android:colorBackground">@color/custom_theme_color</item>
</style>

and then use the custom theme in your manifest

没关系的人,我只是个愚蠢的人,我已经在XML文件中将上述活动的颜色硬编码了,一旦我删除了从XML设置颜色的那行,windowBackground就开始工作了!

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