Domanda

E 'possibile modificare una drawable su un pulsante tramite Java?

Esempio; Ho un pulsante mi piace questo.

<Button 
style="@style/Buttons" 
android:id="@+id/butFavTeam"
android:drawableBottom="@drawable/venue"/>

Voglio cambiare l'immagine drawableBottom corrente con un altro dalla mia directory drawable.

È stato utile?

Soluzione

Hai guardato questo metodo:

/*
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)

Prova questo:

/*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);

Mi auguro che aiuta.

Altri suggerimenti

appena trovato.

Drawable myIcon = this.getResources().getDrawable(R.drawable.myVenue);
butFavTeam.setCompoundDrawablesWithIntrinsicBounds(null, null, null, myIcon);

A proposito ... assicuratevi di non usare accidentalmente i setCompoundDrawables metodo (). Ci vogliono gli stessi parametri setCompoundDrawablesWithIntrinsicBounds (), ma è, ovviamente, in attesa di alcuni dettagli Bounds troppo.

Sono caduto preda di questo perché ho pigramente accettato il primo metodo di immissione facilitata. Sto postando questo nel caso in cui anche voi non riesco a capire il motivo per cui le immagini non vengono visualizzate.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top