简体   繁体   English

某些 android 设备中的背景颜色错误

[英]Wrong background color in some android devices

I'm developing an android application.我正在开发一个 android 应用程序。 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.我已将此颜色设置为其中一种布局(例如“相对布局”)中的背景,并且此颜色在大多数 android 设备中都能正确显示。 BUT in samsung model S6310 android 4.2.1, I have a trouble.但是在三星型号 S6310 android 4.2.1 中,我遇到了麻烦。 My color is not shown and instead of showing this color, android device shows transparent color.我的颜色没有显示,android 设备显示透明颜色而不是显示这种颜色。 I've searched about this problem and the solution is using java code to set color for my layout.我已经搜索过这个问题,解决方案是使用 java 代码为我的布局设置颜色。 but I have many flavor builds and I don't want to change my java code.但我有很多风格构建,我不想更改我的 Java 代码。

Now I have 2 questions:现在我有两个问题:

1- Why is this happening? 1- 为什么会发生这种情况?

2- What's solution for this poblem? 2- 这个问题的解决方案是什么?

Well to answer your question in simple terms好简单的回答你的问题

Why?为什么?

Well, this is simply the work of the Android Support Library.嗯,这只是 Android 支持库的工作。 Android Support Library, now that it is backward compatible it should handle that. Android 支持库,现在它是向后兼容的,它应该处理这个问题。 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.直接在 XML 中设置资源颜色,而不是引用资源。

  2. Also you can set the resource colour or the resource using JAVA Code .您也可以使用JAVA Code设置资源颜色或资源。

  3. It also states that adding它还指出,添加

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

to the build.gradle solves the problem.到 build.gradle 解决了这个问题。 Check here .检查这里

Anyway, better do it programatically.无论如何,最好以编程方式进行。

You can use Android Studio Theme Editor and try changing background color.您可以使用 Android Studio 主题编辑器并尝试更改背景颜色。 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如果它在 android 版本上,你应该检查版本并在那里设置颜色

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

}

Hope this helps if its the problem with the android version.希望这对 android 版本的问题有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM