setBackgroundColor()似乎不起作用

时间:2016-07-04 20:51:36

标签: android

我有一些相当简单的代码。我遍历layoutBIds数组中的每个项目并相应地设置背景的颜色。

setBackgroundColor在bidLayouts上运行良好。

但是当我打电话时

BidDriverPrice.setBackgroundColor(getResources().getColor(R.color.Chartreuse));

它不适用于该视图。我已经尝试了setBackgroundResource,甚至从谷歌搜索结果的前三页中找到了类似的问题,所以我确信我遗漏了一些东西。

for (int i = 0; i < layoutBids.getChildCount(); i++) {
        View v = layoutBids.getChildAt(i);
        RelativeLayout bidLayouts = (RelativeLayout) v.findViewById(R.id.bidlayout);
        final TextView BidDriverPrice = (TextView) v.findViewById(R.id.BidDriverPrice);
        final TextView BidDriverMins = (TextView) v.findViewById(R.id.BidDriverPrice1);
        if (rgxBidOffer.size() != 0) {

            if (rgxBidOffer.get(Integer.parseInt(v.getTag().toString())).seconds > 0) {
                bidLayouts.setBackgroundColor(ContextCompat.getColor(Variables.context, R.color.orange));
            } else {
                bidLayouts.setBackgroundColor(ContextCompat.getColor(Variables.context, R.color.White));
            }

            if (autoBidMode == Setting.AUTO_BID_MODE_CHEAPEST) {
                for (TDriverBids rgxBids2 : rgxBidOffer) {
                    if (rgxBids2.amICheapest) {
                        autoBid = rgxBidOffer.get(Integer.parseInt(v.getTag().toString()));
                        BidDriverPrice.setBackgroundColor(getResources().getColor(R.color.Chartreuse));
                    } else {
                        BidDriverPrice.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                    }

                }
            } else if (autoBidMode == Setting.AUTO_BID_MODE_NEAREST) {
                for (TDriverBids rgxBids2 : rgxBidOffer) {
                    if (rgxBids2.amINearest) {
                        autoBid = rgxBidOffer.get(Integer.parseInt(v.getTag().toString()));
                        BidDriverMins.setBackgroundColor(getResources().getColor(R.color.Chartreuse));
                    } else {
                        BidDriverMins.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                    }

                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试拨打setContentView(R.layout.layout_name); 设置背景颜色后。