问题
I try to set the background color of a TableRow. Currently I have this in my XML file:
android:background="@color/buttonBackground"
and i work great. But when it run
row.setBackgroundColor(R.color.red);
the row disappears. Can someone explain why that is?
回答1:
I believe you need to do:
Resources resource = context.getResources();
row.setBackgroundColor(resource.getColor(R.color.red)
回答2:
You must be missing the alpha value in your color definition. Verify it has 4 bytes, like #FFFFFFFF
.
回答3:
You can also try :
row.setBackgroundColor(Color.RED);
This works for me in Android Studio 2.2.3.
回答4:
Just write: android:background="@android:color/buttonBackground"
来源:https://stackoverflow.com/questions/6077391/set-background-color-of-tablerow