CompoundDrawable tamaño

Android-Lint me da esta pista en algunos casos:

Esta etiqueta y sus hijos pueden ser reemplazados por uno y un compuesto dibujable

Podría hacerlo en algunos lugares, pero en otros lugares donde la ampliación de la imagen es importante que no soy capaz de hacerlo. ¿Hay alguna opción que puedo establecer el tamaño de un compuesto dibujable?

Si escala las imágenes en código como objetos Dibujables, puede configurarlas en código mediante el método setCompoundDrawables() de TextView. Esto requerirá haber llamado setBounds () en los Drawables.

Por lo que sé, no hay forma de establecer el tamaño en XML.

Finalmente publiqué una biblioteca para ello:

https://github.com/a-tolstykh/textview-rich-drawable

Utilice esta biblioteca para establecer el tamaño del compuesto dibujable.

Respuesta original

Puede agregar un atributo personalizado a su vista para trabajar con el tamaño de drawable. Por supuesto, esta solución requiere código adicional, ¡pero sólo una vez! Después de que usted pueda solucionar el problema usando xml solamente (solamente una vez cambie el control y utilícelo en todo el proyecto).

Vea mi ejemplo:

TextViewDrawableSize.java

 Public class TextViewDrawableSize extends TextView {

     Private int mDrawableWidth;
     Private int mDrawableHeight;

     Public TextViewDrawableSize (Context context) {
         Super (contexto);
         Init (contexto, nulo, 0, 0);
     }

     Public TextViewDrawableSize (Context context, AtributoSet attrs) {
         Super (contexto, attrs);
         Init (contexto, attrs, 0, 0);
     }

     Public TextViewDrawableSize (contexto Contexto, AttributeSet attrs, int defStyleAttr) {
         Super (contexto, attrs, defStyleAttr);
         Init (contexto, attrs, defStyleAttr, 0);
     }

     @TargetApi (Build.VERSION_CODES.LOLLIPOP)
     Public TextViewDrawableSize (Contexto contextual, AtributoSet attrs, int defStyleAttr, int defStyleRes) {
         Super (contexto, attrs, defStyleAttr, defStyleRes);
         Init (contexto, attrs, defStyleAttr, defStyleRes);
     }

     Private void init (Contexto contexto, AtributoSet attrs, int defStyleAttr, int defStyleRes) {
         TypedArray array = context.obtainStyledAttributes (attrs, R.styleable.TextViewDrawableSize, defStyleAttr, defStyleRes);

         tratar {
             MDrawableWidth = array.getDimensionPixelSize (R.styleable.TextViewDrawableSize_compoundDrawableWidth, -1);
             MDrawableHeight = array.getDimensionPixelSize (R.styleable.TextViewDrawableSize_compoundDrawableHeight, -1);
         } finalmente {
             Array.recycle ();
         }

         If (mDrawableWidth> 0 || mDrawableHeight> 0) {
             InitCompoundDrawableSize ();
         }
     }

     Private void initCompoundDrawableSize () {
         Drawable [] drawables = getCompoundDrawables ();
         Para (Drawable drawable: drawables) {
             If (drawable == null) {
                 continuar;
             }

             Rect realBounds = drawable.getBounds ();
             Float scaleFactor = realBounds.height () / (float) realBounds.width ();

             Float drawableWidth = realBounds.width ();
             Float drawableHeight = realBounds.height ();

             If (mDrawableWidth> 0) {
                 // guardar el factor de escala de la imagen
                 If (drawableWidth> mDrawableWidth) {
                     DrawableWidth = mDrawableWidth;
                     DrawableHeight = drawableWidth * scaleFactor;
                 }
             }
             If (mDrawableHeight> 0) {
                 // guardar el factor de escala de la imagen

                 If (drawableHeight> mDrawableHeight) {
                     DrawableHeight = mDrawableHeight;
                     DrawableWidth = drawableHeight / scaleFactor;
                 }
             }

             RealBounds.right = realBounds.left + Math.round (drawableWidth);
             RealBounds.bottom = realBounds.top + Math.round (drawableHeight);

             Drawable.setBounds (realBounds);
         }
         SetCompoundDrawables (drawables [0], drawables [1], drawables [2], drawables [3]);
     }
 } 

Attrs.xml

 <resources> <declare-styleable name="TextViewDrawableSize"> <attr name="compoundDrawableWidth" format="dimension"/> <attr name="compoundDrawableHeight" format="dimension"/> </declare-styleable> </resources> 

Y uso

  <com.alt.view.TextViewDrawableSize android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/logo" android:drawableStart="@drawable/logo" android:drawablePadding="@dimen/space_micro" app:compoundDrawableHeight="@dimen/small_logo_height" app:compoundDrawableWidth="@dimen/small_logo_width" /> 

Hay una solución, pero no es 100% satisfactorio ya que no se puede controlar el tamaño directamente sólo el relleno relativo con inset :

Background.xml

 <?xml version="1.0" encoding="utf-8"?> <inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetBottom="4dp" android:insetTop="4dp" android:insetLeft="4dp" android:insetRight="4dp"> <bitmap android:src="@drawable/your_icon"/> </inset> 

Layout.xml

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your text" android:drawableLeft="@drawable/background" android:drawablePadding="8dp" /> 

Otra solución sería ScaleDrawables , pero supongo que esto no funcionará muy bien en diferentes densidades de píxeles.

Sé que esta respuesta es un poco tarde, pero yo recomendaría usar el quickfix: "Añadir ignore 'UseCompoundDrawables' al elemento"

Esto resultará en algo como:

 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" tools:ignore="UseCompoundDrawables" > <!-- This will remove the warning --> <ImageView android:layout_width="@dimen/icon_width" android:layout_height="@dimen/icon_height" android:contentDescription="@string/item1" android:src="@drawable/icon1" /> <TextView android:text="@string/item1" android:textSize="@dimen/label_font_size" /> </LinearLayout> 

Utilícelo con precaución y espere que la API o las herramientas se "arreglen" pronto.

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.