Pregunta

¿Es posible cambiar una estirable en un botón a través de Java?

Ejemplo; Tengo un botón como éste.

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

Quiero cambiar la imagen actual con otro drawableBottom de mi directorio estirable.

¿Fue útil?

Solución

¿Has mirado en 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)

Prueba esto:

/*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 ayude.

Otros consejos

Sólo encontró.

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

Por cierto ... asegúrese de que usted no utiliza accidentalmente los setCompoundDrawables método (). Toma los mismos parámetros que setCompoundDrawablesWithIntrinsicBounds (), pero es obviamente esperando algunos detalles grada también.

Me cayó presa para esto porque perezosamente aceptado el primer método de escritura anticipada. Estoy poniendo esto en caso de que tampoco puede averiguar por qué las imágenes no se muestran.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top