首页 > 解决方案 > 如何从字符串传递 setColor 参数

问题描述

我有一个函数,它接收一个字符串作为名为“颜色”的参数。颜色类似于“FFF267”。现在我想做drawable.setColor(0xF0FFF267)。我该怎么做?我尝试了以下方法,但它引发了异常。

   ImageView circ = (ImageView) findViewById(R.id.circle);
   GradientDrawable drawable = (GradientDrawable) circ.getDrawable();
   drawable.setColor(0xF0 + Integer.toHexString(Integer.parseInt(color)));

标签: javaandroidhex

解决方案


用这个

Color.parseColor("yourcolorstring");


推荐阅读