windowBackground in Android 6 (Marshmallow)

我怕爱的太早我们不能终老 提交于 2020-01-21 04:11:37

问题


I have defined a base style for my application with the following element:

<item name="android:windowBackground">@color/window_background</item>

Which has set the background color for all my activities fine until I tested my app on Android 6 where all backgrounds are white. The backgrounds are still color/window_background on devices running pre-marshmallow.

Anyone know how to make this work (or why it is not working) on Android 6?

Edit with some more info: I am targeting API 22, I have not changed anything from previous version or upgraded the API, just running on Android 6 changes the background.


回答1:


I haven't found anything specific for Marshmallow that would cause this. So the suggestions I have are:

Changing the background color resource to a drawable shape resource.

From:

<item name="android:windowBackground">@color/window_background</item>

To:

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

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/window_background"/>
</shape>

In case you haven't checked the opacity of all your views:

Make sure your windowBackground actually is the background of most of your Activity (particularly over scrollable sections where overdraw is the most important to avoid), removing opaque view backgrounds where possible.

Make your windowBackground work for you instead of using null

I thought this was interesting, to see the precedence of how background layers are set. I am not sure if you are setting any view backgrounds or how you have set up your app, but this is worth a read.

Backgrounds consist of several layers, from back to front:

  • the background Drawable of the theme
  • a solid color (set via setColor(int))
  • two Drawables, previous and current (set via setBitmap(Bitmap) or setDrawable(Drawable)), which may be in transition

BackgroundManager

I can't find if there is a difference with the themes in Marshmallow, or the order of elements, it seems there has been no fundamental changes and I can find no bug for this.

I hope this helps, let me know and I can have another look.

If this doesn't help it may be worth posting some more code relevant to the problem. Cheers.




回答2:


I used to have a same problem, but I found out by trying that if I commented actionBarTheme in my styles, it started to work suddenly. I dug deeper in my styles and found out that the style of action bar was setting a android:background attribute after commenting it out everything works now as expected.




回答3:


If you're using Android Studio 1.4 or higher go to styles where your theme is located and click "Open Editor" in the upper right hand corner. Then change your window background there. It should be under "android:colorBackground"




回答4:


How about setting both windowBackground and colorBackground

<item name="android:windowBackground">@color/window_background</item>
<item name="android:colorBackground">@color/window_background</item>


来源:https://stackoverflow.com/questions/32995079/windowbackground-in-android-6-marshmallow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!