Is there any shortcut on Android Studio to get the

2019-09-05 16:57发布

问题:

I don't want to remember the color codes during coding. Is there is any shortcut to get the color codes from the color name or anything else to get the color code on Android Studio ?

回答1:

You can use a colors.xml where you store your colors. Then you can access them later in xml as @color/myGreen.

Here is a example xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="my_green">#00cc00</color>
</resources>

If you need the color again as int you can use this code:

int color = context.getResources().getColor(R.color.my_green)

Or better to avoid deprecated methods:

Resources res = context.getResources();
int color = res.getColor(R.color.my_green, res.getTheme());