如何从android中的颜色标签中获取颜色代码

问题描述 投票:-1回答:2

有没有办法获得颜色代码例如

   <resources>
        <color name="colorPrimary">#ff000000</color>
        <color name="colorPrimaryDark">#ff000000</color>
        <color name="colorAccent">#ff008dcd</color>
        <color name="colorControlHighlight">#ff757575</color>
        <color name="colorControlNormal">#ff57beee</color>
        </resources>

所以我想得到颜色名称和颜色代码

我们来看第一个代码所以它是colorPrimary,代码是#ff000000

注意此代码不是color.xml,但它是一个字符串,我想从中获取名称和颜色代码

java android xml
2个回答
0
投票

没有alpha,你可以这样做:

int colorPrimary = getResources().getColor(R.color.colorPrimary);
String strColorPrimary = "#"+Integer.toHexString(colorPrimary);

0
投票

类似的问题在这里回答:Android get list of string resources from special file

因此,您可以尝试枚举案例中的颜色资源,以查找与特定值匹配的名称。

© www.soinside.com 2019 - 2024. All rights reserved.