如何在安卓系统中设置RGB颜色?

17

我正在尝试使用text1.setBackgroundColor(Color.rgb(r,g,b));设置文本背景颜色

从数据库中获取红、绿、蓝值基于值i,将r=128,g=255,b=128编程设置为它是暗绿色,但当我运行程序时,它显示为红色。

for(int i = 0; i < list.length(); i++){
            JSONObject c = list.getJSONObject(i);

            // Storing each json item in variable
           // String GRPCODE = c.getString(TAG_GRPCODE);
            String GRPNAME = c.getString(TAG_GRPNAME);
            String QTY = c.getString(TAG_QNT);
            String BUDGET = c.getString(TAG_BUDGET);
            String STOCK = c.getString(TAG_STOCK);
            String DIFF = c.getString(TAG_DIFF);
            String DIFF_P = c.getString(TAG_DIFF_P);

             COLOR = c.getString(TAG_COLOR);

            /*String[] ARGB = COLOR.split(" ");

             String V1=ARGB[0];
             String V2=ARGB[1];
             String V3=ARGB[2];
             String V4=ARGB[3];

             a=Integer.parseInt(V1);
             r=Integer.parseInt(V2);
             g=Integer.parseInt(V3);
             b=Integer.parseInt(V4);*/


            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
          //  map.put(TAG_GRPCODE, GRPCODE);
            map.put(TAG_GRPNAME, GRPNAME);

            map.put(TAG_QNT, QTY);
            map.put(TAG_BUDGET, BUDGET);
            map.put(TAG_STOCK, STOCK);
            map.put(TAG_DIFF, DIFF);
            map.put(TAG_DIFF_P, DIFF_P);

            map.put(TAG_COLOR,COLOR);
            //map.put(TAG_PHONE_MOBILE, mobile);

            // adding HashList to ArrayList
            contactList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    /**
     * Updating parsed JSON data into ListView
     * */
     ListAdapter adapter = new SimpleAdapter(this, datatList,
            R.layout.list_item,
            new String[] {  TAG_GRPNAME, TAG_QNT, TAG_BUDGET, TAG_STOCK, TAG_DIFF, TAG_DIFF_P, },
            new int[] {
                     R.id.l2, R.id.l3, R.id.l4, R.id.l5, R.id.l6, R.id.l7}){
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
             View v = convertView;
             if (v == null) {
                 LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 v = vi.inflate(R.layout.list_item, null);
             }
              text1 = (TextView) v.findViewById(R.id.l7);


             HashMap<String, String> map=dataList.get(position); 
             map.get(TAG_COLOR); 
             String[] ARGB = COLOR.split(" "); 
             String V1=ARGB[0]; 
             String V2=ARGB[1]; 
             String V3=ARGB[2]; 
             String V4=ARGB[3]; 
             a=Integer.parseInt(V1); 
             r=Integer.parseInt(V2); 
             g=Integer.parseInt(V3); 
             b=Integer.parseInt(V4); 

             text1.setBackgroundColor(Color.rgb(r,g,b));

             return super.getView(position, v, parent);
         }
     };

setbackgroundcolor 之前添加一个日志语句,检查值是否符合要求。 - Onur A.
@OnurA. 在设置颜色之前,我会检查它是否正确。 - ibu
2
你的视图颜色都一样吗?因为在for循环中,每次r、g、b值都在变化,所以你的视图使用了数据库中最后一个颜色值。 - Onur A.
1
从现有代码中删除颜色解析代码,然后您应该在getView()中重新解析该颜色值。HashMap<String, String> map=contactList.get(position); map.get(TAG_COLOR); String[] ARGB = COLOR.split(" "); String V1=ARGB[0]; String V2=ARGB[1]; String V3=ARGB[2]; String V4=ARGB[3]; a=Integer.parseInt(V1); r=Integer.parseInt(V2); g=Integer.parseInt(V3); b=Integer.parseInt(V4);text1.setBackgroundColor(Color.rgb(r,g,b)); - Onur A.
@OnurA,抱歉它出现了相同的问题。 - ibu
显示剩余6条评论
1个回答

25

最终我得到了答案

ListAdapter adapter = new SimpleAdapter(this, dataList,
            R.layout.list_item,
            new String[] {  TAG_GRPNAME, TAG_QNT, TAG_BUDGET, TAG_STOCK, TAG_DIFF, TAG_DIFF_P, },
            new int[] {
                     R.id.l2, R.id.l3, R.id.l4, R.id.l5, R.id.l6, R.id.l7}){
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
             View v = convertView;
             if (v == null) {
                 LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 v = vi.inflate(R.layout.list_item, null);
             }
              text1 = (TextView) v.findViewById(R.id.l7);

              String rgbColor=  dataList.get(position).get(TAG_COLOR);
              String[] ARGB = rgbColor.split(" ");     
              a=Integer.parseInt(ARGB[0]);
              r=Integer.parseInt(ARGB[1]);
              g=Integer.parseInt(ARGB[2]);
              b=Integer.parseInt(ARGB[3]);                        
              text1.setBackgroundColor(Color.rgb(r, g, b));
              return super.getView(position, v, parent);
          }

     };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接