Mudando DrawableBottom com Java
Pergunta
É possível alterar um punhado em um botão via Java?
Exemplo; Eu tenho um botão como este.
<Button
style="@style/Buttons"
android:id="@+id/butFavTeam"
android:drawableBottom="@drawable/venue"/>
Quero alterar a imagem atual do drawableBottom com outra do meu diretório desenhado.
Solução
Você já olhou para este método:
/*
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)
Experimente isso:
/*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);
Espero que isso ajude.
Outras dicas
Acabei de encontrar.
Drawable myIcon = this.getResources().getDrawable(R.drawable.myVenue);
butFavTeam.setCompoundDrawablesWithIntrinsicBounds(null, null, null, myIcon);
A propósito ... certifique -se de não usar acidentalmente o método setCompoundDrawables (). Ele leva os mesmos parâmetros que o setCompoundDrawablesWithIntrinsicBounds (), mas obviamente está esperando alguns detalhes de limites também.
Fui vítima por isso porque aceitei preguiçosamente o primeiro método de tipo de tipo. Estou postando isso caso você também não consiga descobrir por que as imagens não estão exibindo.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow