setting ImageButton's background transparent with Theme.Light in android
How to set the background color of an Image开发者_Python百科Button 'transparent' ?
The following piece of code works when I try it with default android theme, but with 'Theme.Light', I see a gray background.
ImageButton deleteBtn = new ImageButton( this );
deleteBtn.setImageResource( R.drawable.delete_big );
deleteBtn.setBackgroundColor(android.R.color.transparent);
(I need to dynamically create these buttons.. I have seen solutions which mention specifying @null background color using android's layout.xml files. How can I achieve the same thing programmatically ? Any help highly appreciated. Thanks!
I had to do something similar to this and I used a transparent png 9patch. Then you can set it to the background with something like:
deleteBtn.setBackgroundResource(R.drawable.transparent_bground);
Edit: here is a 9 patch you can use. Save it in one of your res/drawable folders.
Even though this is kind of answered, here's what went wrong.
Watch out for colors. android.R.color.transparent
actually is an integer referencing a lookup table, and every time you set a color in Android in Java, it takes in an integer that represents the color.
Now, this is the tricky part: What's happening here is that it's setting the bg color to the lookup table index, which happens to translate to gray. If I remember right, getResources().getColor(id)
is a function that can be called from a context or activity to get your correct color.
精彩评论