我想在 Android Studio.But 中转换一些 Color Hex,其中一些会抛出错误?如何解决?

I want to convert some Color Hex in Andorid Studio.But, some of them throw an error?How to fix it?

我使用 Retrofit 从 api.Whosebug 中检索了一些数据。这里有一些: #FFF、#666、#5A8F53、#1B8FBB。然后我想使用它们 textView.setBackgroundColor 属性。所以,我用Color.parseColor()method.But,出现了未识别颜色的错误。如何解决这个问题?

    @Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view=layoutInflater.inflate(R.layout.p203customrow,parent,false);   
    ImageView imageViewP203= (ImageView)view.findViewById(R.id.imageViewP203);
    TextView textViewNameP203= (TextView)view.findViewById(R.id.textViewNameP203);
    TextView textViewSiteUrlP203= (TextView)view.findViewById(R.id.textViewSiteUrlP203);

    P203ApiStyle.P203ItemsObjects p203ItemsObjects= p203ItemsObjectsList.get(position);
    Map<String,String> mapStyle=p203ItemsObjects.getStyling();
    String backgroundColor= mapStyle.get("tag_background_color");
    String foregroundColor= mapStyle.get("tag_foreground_color");

    textViewNameP203.setText("Name="+p203ItemsObjects.getName()+" BackgroundColor="+backgroundColor);  
//one result--> Name=Webmasters BacgroundColor=#FFF
    textViewSiteUrlP203.setText("SiteUrl=" + p203ItemsObjects.getSite_url() + " BackgroundColor=" + foregroundColor);
 //one result-->SiteUrl=The url BackgoundColor=#1B8FBB

//when I uncommet to this block that error occurs...
  /*  textViewNameP203.setBackgroundColor(Color.parseColor(backgroundColor));
    textViewSiteUrlP203.setBackgroundColor(Color.parseColor(foregroundColor));*/

    Picasso.with(context).load(p203ItemsObjects.getIcon_url()).resize(100, 100).into(imageViewP203);
    return view;
}

将网络安全颜色更改为十六进制颜色
//if 声明某些网络安全颜色以更改它的十六进制颜色 exp #FFF --> #FFFFFF 如果(backgroundColor.length()==4){
char harf= backgroundColor.charAt(1); 字符串后缀=String.valueOf(harf)+String.valueOf(harf)+String.valueOf(harf); backgroundColor=backgroundColor.concat(后缀); } textViewNameP203.setBackgroundColor(Color.parseColor(背景颜色));

正如我在评论中提到的,在您的代码中,您使用的是网络安全颜色而不是 HEX 颜色。

A hexadecimal color is specified with: #RRGGBB, where the RR (red), GG (green) and BB (blue) hexadecimal integers specify the components of the color. All values must be between 00 and FF.

例如#0000FF值呈现为蓝色,因为蓝色分量设置为最高值(FF),其他设置为最低值(00) .

因此您必须将网络安全颜色更改为十六进制颜色。 在您的代码中,请将 #FFF 更改为 #FFFFFF 并将 #666 更改为 #666666 .

请点击此处查看the available colors sorted by name.

并在此处查看 the material color codes.