如何从Android主题中提取颜色值(#rgb) ?

时间:2022-07-07 21:23:44

I want to use colors from a Theme to apply it to some HTML my app is rendering. I am wondering if I can do that?

我想用主题的颜色来应用到我的应用渲染的HTML中。我想知道我能不能那样做?

I am looking to use colors like they are specified in themes.xml:

我希望使用主题中指定的颜色。xml:

    <item name="colorBackground">@android:color/background_dark</item>
    <item name="textColorPrimary">@android:color/primary_text_dark</item>

So looking at them they are declared in the same way. So I thought I could access them in the same way too.

所以看着它们,它们是用同样的方式声明的。所以我想我也可以用同样的方式访问它们。

That is not the cause though. When trying to access those values this way:

但这不是原因。当尝试以这种方式访问这些值时:

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(android.R.attr.colorBackground, tv, true);

    System.out.println("tv.string=" + tv.string);
    System.out.println("tv.coerced=" + tv.coerceToString());

    int colorResourceId = getResources().getColor(tv.resourceId);
    System.out.println("colorResourceId=" + colorResourceId);

    tv = new TypedValue();
    getTheme().resolveAttribute(android.R.attr.textColorPrimary, tv, true);

    System.out.println("tv.string=" + tv.string);
    System.out.println("tv.coerced=" + tv.coerceToString());

    colorResourceId = getResources().getColor(tv.resourceId);
    System.out.println("colorResourceId=" + colorResourceId);

I get this as a result:

我得到的结果是:

I/System.out( 1578): tv.string=null
I/System.out( 1578): tv.coerced=#ffffffff 
I/System.out( 1578): colorResourceId=-1

I/System.out( 1578): tv.string=res/color/primary_text_light.xml
I/System.out( 1578): tv.coerced=res/color/primary_text_light.xml
I/System.out( 1578): colorResourceId=-16777216

The results are different. The first one actually gives me the color "#fffffff" which would work for me, the second one only gives me an xml.

结果是不同的。第一个实际上给了我颜色“#fffffff”,这对我来说很有用,第二个只给我一个xml。

Do I need to jump through a few more hoops here to resolve the actual color? Does my original intention work at all? Maybe it won't work, because colors could be arbitrary drawables?

我是否需要跳过这里的一些障碍来解决实际的颜色?我最初的想法行得通吗?也许它行不通,因为颜色可能是任意可画的?

I didn't find any relevant documentation, but if you know any, just point me there please.

我没有找到任何相关的文件,但是如果你知道的话,请告诉我。

Btw. I also tried obtainStyledAttributes(), but this had basically the same issues.

顺便说一句。我也尝试了obtainStyledAttributes(),但这基本上是相同的问题。

1 个解决方案

#1


6  

I think you should rename colorResourceId to myColor or something like that, because that's what it's supposed to be in your code, as far as I can tell.

我认为你应该将colorResourceId重命名为myColor或者类似的东西,因为就我所知,它应该在你的代码中。

-16777216 is equivalent with 0xFF000000, which is black color, so probably your theme was black text on a white background.

-16777216和0xFF000000是等价的,0xFF000000是黑色的,所以您的主题可能是白色背景上的黑色文本。

#1


6  

I think you should rename colorResourceId to myColor or something like that, because that's what it's supposed to be in your code, as far as I can tell.

我认为你应该将colorResourceId重命名为myColor或者类似的东西,因为就我所知,它应该在你的代码中。

-16777216 is equivalent with 0xFF000000, which is black color, so probably your theme was black text on a white background.

-16777216和0xFF000000是等价的,0xFF000000是黑色的,所以您的主题可能是白色背景上的黑色文本。