可以用字符串获得颜色?

kue*_*e01 3 string android colors android-resources

在我的values文件夹中,我有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>
Run Code Online (Sandbox Code Playgroud)

有没有办法让这些颜色只是用它的名字串?就像是view.setBackgroundColor.getColor("orange1");

对于图像,你有这个 getResources().getIdentifier("my_image", "drawable", getPackageName());

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

mih*_*024 6

您是否尝试过以下方法:

int desiredColour = getResources().getColor(getResources().getIdentifier("my_color", "color", getPackageName()));
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!