代码
public String getHexColor(int id) {
StringBuilder result = new StringBuilder();
int color = ContextCompat.getColor(requireContext(), id);
result.append("#");
result.append(intToHexValue(Color.red(color)));
result.append(intToHexValue(Color.green(color)));
result.append(intToHexValue(Color.blue(color)));
return result.toString();
}
private String intToHexValue(int number) {
StringBuilder result = new StringBuilder();
result.append(Integer.toHexString(number & 0xff));
while (result.length() < 2) {
result.append("0");
}
return result.toString().toUpperCase();
}
使用
getHexColor(R.color.status_weak);
|