Changing drawableBottom with java
Is it possible to change a drawable on a button via java?
Example; I have a button like this.
<Button
style="@style/Buttons"
android:id="@+id/butFavTeam"
android:drawableBottom="@drawable/venue"/>
I wan开发者_开发知识库t to change the current drawableBottom image with another from my drawable directory.
Have you looked at this method:
/*
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
*/
public void setCompoundDrawablesWithIntrinsicBounds (Drawable left, Drawable top, Drawable right, Drawable bottom)
Try this:
/*remember to first clear the callback of the drawable you are replacing to prevent memory leaks...
* Get the Drawables for the button by calling: myButton.getCompoundDrawables(), then loop through that calling myOldDrawable.setCallback(null);
* e.g:*/
for(Drawable myOldDrawable : myButton.getCompoundDrawables())
{
myOldDrawable.setCallback(null);
}
//use null where you don't want a drawable
myButton.setCompoundDrawablesWithIntrinsicBounds(null,null,null, myNewDrawable);
I hope that helps.
Just found it.
Drawable myIcon = this.getResources().getDrawable(R.drawable.myVenue);
butFavTeam.setCompoundDrawablesWithIntrinsicBounds(null, null, null, myIcon);
By the way... make sure you don't accidentally use the method setCompoundDrawables(). It takes the same parameters as setCompoundDrawablesWithIntrinsicBounds(), but it's obviously expecting some bounds details too.
I fell prey for this because I lazily accepted the very first type-ahead method. I'm posting this in case you too cannot figure out why the images aren't displaying.
精彩评论