Después de llamar al setCompoundDrawables
método, no se muestra el Drawable compuesto.
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);
¿Alguna idea?
fuente
Después de llamar al setCompoundDrawables
método, no se muestra el Drawable compuesto.
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);
¿Alguna idea?
Necesitaba estar usando setCompoundDrawablesWithIntrinsicBounds
.
Use esto (lo probé). Funciona bien
Drawable image = context.getResources().getDrawable( R.drawable.ic_action );
int h = image.getIntrinsicHeight();
int w = image.getIntrinsicWidth();
image.setBounds( 0, 0, w, h );
button.setCompoundDrawables( image, null, null, null );
EditText#setCompoundDrawablesWithIntrinsicBounds
requiere al menos API 17.
La imagen está en blanco porque no tiene límites especificados. Puede usar, setCompoundDrawables()
pero antes de especificar los límites de la imagen, use el Drawable.setBounds()
método
Ejemplo establecido en la parte superior:
view.setCompoundDrawablesWithIntrinsicBounds(
null,
getResources().getDrawable(R.drawable.some_img),
null,
null
);
orden de argumentos: (izquierda, arriba, derecha, abajo)
Un poco más simple de nuevo:
Drawable image = context.getResources().getDrawable(R.drawable.ic_action );
image.setBounds( 0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight() );
button.setCompoundDrawables( image, null, null, null );
Está en desuso en API 22.
Este código es útil para mí:
Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.wen, null);
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
tv.setCompoundDrawables(drawable, null, null, null);
En Kotlin:
1) Conjunto drawable
:
val drawable = ContextCompat.getDrawable(context!!,R.drawable.ic_image)?.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}
o
val drawable = ResourcesCompat.getDrawable(resources, R.drawable.ic_image, null)?.apply {
setBounds(0, 0, minimumWidth, minimumHeight)
}
2) Conjunto TextView
:
textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
o
button.setCompoundDrawables(null, drawable, null, null)
setCompoundDrawablesWithIntrinsicBounds
funcionó ..
Para mí, setCompoundDrawablesWithIntrinsicBounds (Drawable, Drawable, Drawable, Drawable) no funcionó.
Tuve que usar setCompoundDrawablesWithIntrinsicBounds (0, 0, 0, 0) .
Ejemplo con Kotlin:
val myView = layoutInflater.inflate(R.layout.my_view, null) as TextView
myView.setCompoundDrawablesWithIntrinsicBounds(0, myDrawable, 0, 0)
La imagen no se muestra porque no especificó los límites, por lo que tiene 2 opciones aquí.
1er método
Use el setCompoundDrawablesWithIntrinsicBounds
método, como se muestra a continuación
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn. setCompoundDrawablesWithIntrinsicBounds(myDrawable, null, null, null);
2do método
Puede aplicar límites al dibujable antes de aplicar a TextView, como se muestra a continuación
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
myDrawable.setBounds( 0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
btn.setCompoundDrawables(myDrawable, null, null, null);
Eso es.
(..)WithIntrinsicBounds
debe llamarse a la variante del método nombrado . En una nota al margen,padding
para que el Compuesto Dibujable se debe configurar después de esta llamada para causar un efectosetBounds(Rect)
llamado.