Android TextVies:设置背景颜色动态显示,但是不起作用

给一个android的TextView设置背景颜色动态显示,到那时好像不起作用。我是少了什么东西么?

TextView et = new TextView(activity);
et.setText("350");
et.setBackgroundColor(R.color.white);
I also have this file (colors.xml) in my res/values folder

在我的res/values文件夹中还有这个文件(colors.xml)

<resources>
        <color name="white">#ffffffff</color>
        <color name="black">#ff000000</color>
</resources>

而且,设置文本的颜色引起了TextView消失。

TextView c1 = new TextView(activity);
c1.setTextColor(R.color.solid_red);
c1.setText("My Text");

试试这个:

TextView c1= new TextView(activity);
c1.setTextColor(getResources().getColor(R.color.solid_red));
c1.setText("My Text");

我同意颜色和资源应该有相同的样式,但是我也花费了一些时间才找到这个解决方法。

et.setBackgroundResource(R.color.white); 

设置红色为

textView.setBackgroundColor(0xfff00000);

或者是

<color name="solid_red">#fff00000</color>textView.setBackgroundResource(R.color.solid_red);

setBackgroundColor 传入的参数是十六进制的颜色值,你可以使用setBackgroundResource(R.color.white)