Java 我想在Andorid Studio中转换一些颜色十六进制。但是,其中一些会抛出错误?如何修复?

Java 我想在Andorid Studio中转换一些颜色十六进制。但是,其中一些会抛出错误?如何修复?,java,android,colors,hex,Java,Android,Colors,Hex,我使用改型从api.stackoverflow检索了一些数据。以下是其中一些: #FFF、666、5A8F53、1B8FBB。然后我想使用textView.setBackgroundColor属性。所以,我使用Color.parseColor()方法。但是,有一个错误是“未识别的颜色”。如何解决这个问题 @Override public View getView(int position, View convertView, ViewGroup parent) { Layout

我使用改型从api.stackoverflow检索了一些数据。以下是其中一些: #FFF、666、5A8F53、1B8FBB。然后我想使用textView.setBackgroundColor属性。所以,我使用Color.parseColor()方法。但是,有一个错误是“未识别的颜色”。如何解决这个问题

    @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;
}
@覆盖
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
视图=LayoutFlater.充气(R.layout.p203customrow,父级,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(位置);
Map mapStyle=p203ItemsObjects.GetStyleing();
String backgroundColor=mapStyle.get(“tag_background_color”);
String foregroundColor=mapStyle.get(“标记前景颜色”);
textViewNameP203.setText(“Name=“+p203ItemsObjects.getName()+”BackgroundColor=“+BackgroundColor”);
//一个结果-->名称=网站管理员BacgroundColor=#FFF
textViewSiteUrlP203.setText(“SiteUrl=“+p203ItemsObjects.getSite_url()+”BackgroundColor=“+foregroundColor”);
//一个结果-->SiteUrl=url BackgoundColor=#1B8FBB
//当我取消此块的连接时,会发生错误。。。
/*textViewNameP203.setBackgroundColor(Color.parseColor(backgroundColor));
textViewSiteUrlP203.setBackgroundColor(Color.parseColor(foregroundColor))*/
毕加索.with(context).load(p203ItemsObjects.getIcon_url()).resize(100100).into(imageViewP203);
返回视图;
}

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

正如我在评论中提到的,在代码中,您使用的是websafe颜色而不是十六进制颜色

十六进制颜色指定为:
#RRGGBB
,其中
RR
(红色)、
GG
(绿色)和
BB
(蓝色)十六进制整数指定 颜色。所有值必须介于
00
FF
之间

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

因此,您必须将websafe颜色更改为十六进制颜色。 在代码中,请将
#FFF
更改为
#ffff
,将
#666
更改为
#666666

请在这里查看


我补充道:“请点击此处查看

,您的代码在哪里?”。对不起,您使用的是websafe颜色,不是十六进制。所以把#fff换成#ffffff,#666换成#666666我不知道websafe的颜色。非常感谢。我添加了这个代码块来解决我的问题。我已经这样做了#从FFF到#FFFFFF的编程,但是;我没有找到一种方法将网络安全颜色转换为十六进制颜色。我可以将十六进制颜色转换为整数值,然后将其用作背景色。示例:#006699-->红色:0,绿色:102,蓝色:153并使用它的颜色。rgb(红色,绿色,蓝色)是。可以那么,如果我的回答帮助你解决了问题,请将其标记为已接受的答案。