0

我正在尝试更改文本视图的背景颜色,如下所示:

    for(int i=0; i<arrBool.length; i++) {
        arrBool[i] = r.nextBoolean();
        if(arrBool[i]==true) {
            textView[i].setBackgroundResource(R.color.darkgrey);
        }
    }

我收到 R.color.darkgrey 的错误。请帮我解决一下这个。

4

4 回答 4

1

您需要提供getResources()方法,然后再提供调用getColor()方法。

textView[i].setBackgroundResource(getResource().getColor(R.color.darkgrey));
                                  ^^^^^^^^^^^^^
于 2012-10-31T04:24:58.873 回答
0

在 color.xml 中使用颜色时,您必须调用该getResource()方法。

将您的代码更改为

for(int i=0; i<arrBool.length; i++) {
    arrBool[i] = r.nextBoolean();
    if(arrBool[i]==true) {
        textView[i].setBackgroundResource(getResource().getColor(R.color.darkgrey));
    }
}
于 2012-10-31T04:23:28.673 回答
0

设置背景颜色,如果它的颜色是:

textView[i].setBackgroundColor(getResources().getColor(R.color.darkgrey));
于 2012-10-31T04:51:34.067 回答
0

正如其他人所说,如果代码是活动的一部分,您可能应该使用。

for(int i=0; i<arrBool.length; i++) {
    arrBool[i] = r.nextBoolean();
    if(arrBool[i]==true) {
        textView[i].setBackgroundResource(getResource().getColor(R.color.darkgrey));
    }
}

请注意getResource()是一种ContextWrapper使用方法

mContext.getResource().getColor(R.color.darkgrey)

如果需要,mContexta在哪里Context

于 2012-10-31T04:28:44.387 回答