Is there any shortcut on Android Studio to get the

2019-09-05 16:54发布

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条回答
Root(大扎)
2楼-- · 2019-09-05 17:01

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());
查看更多
登录 后发表回答