Define a custom color variable

后端 未结 3 1503
梦毁少年i
梦毁少年i 2021-02-12 11:50

I want to change some button colors globally in my code. I cannot seem to fine a way to define a color variable and then assign a color value to that variable.

I tried

相关标签:
3条回答
  • 2021-02-12 12:12

    you can use values/colors.xml. For instance

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="menu_background">#666666</color>
    </resources>
    
    0 讨论(0)
  • 2021-02-12 12:20

    You must add attribute type="color" in the color tag:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color type="color" name="menu_background">#666666</color>
    </resources>
    

    So, you can use the color in xml file as "@color\menu_background" and also from java code.

    0 讨论(0)
  • 2021-02-12 12:24

    "I cannot seem to fine a way to define a color variable and then assign a color value to that variable."

    Here is how you can define a color variable:

    int selectedColor = Color.rgb(0, 0, 100);
    

    and use it:

    f1.setBackgroundColor(selectedColor);
    
    0 讨论(0)
提交回复
热议问题