有个需求是需要传十六进制色值到后台,因为颜色值都配置在Colors.xml,所以需要转换一下,方法如下
colors.xml文件内容
<color name="style_color">#67ac66</color>
调用方法
changeColor(mContext,R.color.style_color)
/得到内容 67ac66
/
public String changeColor(Context context,int id){
StringBuffer stringBuffer = new StringBuffer();
int color = context.getResources().getColor(id);
int red = (color & 0xff0000) >> 16;
int green = (color & 0x00ff00) >> 8;
int blue = (color & 0x0000ff);
stringBuffer.append(Integer.toHexString(red)); stringBuffer.append(Integer.toHexString(green)); stringBuffer.append(Integer.toHexString(blue)); LogUtils.d("changecolor="+stringBuffer.toString()); return stringBuffer.toString(); }
原文链接:https://www.f2er.com/xml/294437.html