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());