<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- color int as #AARRGGBB (alpha, red, green, blue) -->
<color name="orange">#fff3632b</color>
...
<color name="my_view_color">@color/orange</color>
</resources>
获取color int并设置它
int backgroundColor = ContextCompat.getColor(context, R.color.my_view_color);
// Color backgroundColor = ... (Don't do this. The color is just an int.)
myView.setBackgroundColor(backgroundColor);
public enum Colors
{
COLOR0(0x26, 0x32, 0x38), // R, G, B
COLOR1(0xD8, 0x1B, 0x60),
COLOR2(0xFF, 0xFF, 0x72),
COLOR3(0x64, 0xDD, 0x17);
private final int R;
private final int G;
private final int B;
Colors(final int R, final int G, final int B)
{
this.R = R;
this.G = G;
this.B = B;
}
public int getColor()
{
return (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
}
public int getR()
{
return R;
}
public int getG()
{
return G;
}
public int getB()
{
return B;
}
}
int alpha = ... // 0-255, calculated based on some business logic
int actionBarBackground = getResources().getColor(R.color.actionBarBackground);
int actionBarBackgroundWithAlpha = Color.argb(
alpha,
Color.red(actionbarBackground),
Color.green(actionbarBackground),
Color.blue(actionbarBackground)
);