Set background color of TableRow
I try to set the background color of a TableRow. Currently I 开发者_Python百科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?
I believe you need to do:
Resources resource = context.getResources();
row.setBackgroundColor(resource.getColor(R.color.red)
You can also try :
row.setBackgroundColor(Color.RED);
This works for me in Android Studio 2.2.3.
You must be missing the alpha value in your color definition. Verify it has 4 bytes, like #FFFFFFFF
.
row.setBackgroundColor(ContextCompat.getColor(this,R.color.colorAccent))
Just write: android:background="@android:color/buttonBackground"
精彩评论