可以用线串弄颜色吗? [英] Possible to get color with string?

查看:90
本文介绍了可以用线串弄颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的values文件夹中,我有my_colors.xml:

In my values folder I have my_colors.xml:

<resources>
    <!-- Orange -->
    <color name="orangePrimary">#f6a02d</color>
    <color name="orange1">#e3952a</color>
    <color name="orange2">#da8f28</color>
    <color name="orange3">#d08926</color>
</resources>

是否有一种方法可以仅使用其名称的字符串来获得这些颜色? 类似于view.setBackgroundColor.getColor("orange1");

Is there a way to get these colors just with the string of its name? Something like view.setBackgroundColor.getColor("orange1");

对于图像,您具有getResources().getIdentifier("my_image", "drawable", getPackageName());

希望你们知道我的意思. 问候.

Hope you guys know what I mean. Greetings.

推荐答案

您是否尝试过以下方法:

Have you tried the following:

int desiredColour = getResources().getColor(getResources().getIdentifier("my_color", "color", getPackageName()));

希望有帮助!

这篇关于可以用线串弄颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆