2011-03-20 79 views
0

下面的代码将背景颜色更改为黑色,无论我以何种颜色通过 参数。如何在运行时在Android中更改Button的BackgroundColor?

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    b = (Button)findViewById(R.id.Button01); 
    b.setBackgroundColor(R.color.red); 
} 

任何帮助?

回答

0

使用本:

getResources().getColor() 

类似如下:

b.setBackgroundColor(getResources().getColor(R.color.red)); 
0

我知道,它确实是一个迟到的回答,但我只是你的问题遇到了

可以使用

b.setBackgroundColor(android.R.color.white) 

b.setBackgroundColor(Color.RED);