简体   繁体   中英

Wrong background color in some android devices

I'm developing an android application. I've defined a color in my resource as this:

  <color name="toolbar_color">#ffcb00</color>

I've set this color as background in one of layouts such as 'Relative layout' and this color is shown correctly in most of android devices. BUT in samsung model S6310 android 4.2.1, I have a trouble. My color is not shown and instead of showing this color, android device shows transparent color. I've searched about this problem and the solution is using java code to set color for my layout. but I have many flavor builds and I don't want to change my java code.

Now I have 2 questions:

1- Why is this happening?

2- What's solution for this poblem?

Well to answer your question in simple terms

Why?

Well, this is simply the work of the Android Support Library. Android Support Library, now that it is backward compatible it should handle that. It should make the Application read those resources properly. But sometimes it doesn't. That mainly causes this. You can read more about how to set resources if something like that happens here and here .

Work-arounds Well there are many workarounds to this,

  1. Set the resource colour directly in the XML instead of referencing to a resource.

  2. Also you can set the resource colour or the resource using JAVA Code .

  3. It also states that adding

    android { ... defaultConfig { ... vectorDrawables.useSupportLibrary = true } ... }

to the build.gradle solves the problem. Check here .

Anyway, better do it programatically.

You can use Android Studio Theme Editor and try changing background color. You can get more info on Theme editor here and here .

if its on android version you should check the version and set the color there

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLYBEAN) {
// Your required color
} else {

}

Hope this helps if its the problem with the android version.

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