Configurar programáticamente el elemento de diseño izquierdo en una vista de texto


Tengo un TextView en xml aquí.

<TextView
        android:id="@+id/bookTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableLeft="@drawable/checkmark"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textSize="24dip"
        android:maxLines="1"
        android:ellipsize="end"/>

Como puede ver, configuré el drawableLeft en xml.

Me gustaría cambiar el elemento de diseño en el código.

¿Hay alguna manera de hacer esto? ¿O establecer el drawableLeft en código para la vista de texto?

Author: Cœur, 2011-08-03

4 answers

Puedes usar setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)

Establecer 0 donde no desea imágenes

Ejemplo para Drawable a la izquierda:

TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

Consejo: Siempre que conozca algún atributo XML pero no tenga idea de cómo usarlo en tiempo de ejecución. simplemente vaya a la descripción de esa propiedad en developer doc. Allí encontrará Métodos relacionados si está soportado en tiempo de ejecución . es decir, Para drawableLeft

 642
Author: BrainCrash,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-12 12:13:27

De aquí veo el método setCompoundDrawablesWithIntrinsicbounds(int,int,int,int) se puede utilizar para hacer esto.

 14
Author: Jack,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-03 19:25:35

Puede usar cualquiera de los siguientes métodos para configurar el elemento de diseño en TextView:

1- setCompoundDrawablesWithIntrinsicbounds(int, int, int, int)

2- setCompoundDrawables (Left_Drawable, Top_Drawable, Right_Drawable, Bottom_Drawable)

Y para obtener elementos de diseño de los recursos que puede utilizar:

getResources().getDrawable(R.drawable.your_drawable_id);
 5
Author: Shajeel Afzal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-05 08:01:30
static private Drawable **scaleDrawable**(Drawable drawable, int width, int height) {

    int wi = drawable.getIntrinsicWidth();
    int hi = drawable.getIntrinsicHeight();
    int dimDiff = Math.abs(wi - width) - Math.abs(hi - height);
    float scale = (dimDiff > 0) ? width / (float)wi : height /
            (float)hi;
    Rect bounds = new Rect(0, 0, (int)(scale * wi), (int)(scale * hi));
    drawable.setBounds(bounds);
    return drawable;
}
 -8
Author: Rashmi P,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-15 06:29:54