Textview setCompoundDrawables no cambiar o mostrar

Tengo un ListView con un elemento de lista personalizada

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@android:id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/app_name" android:maxWidth="32dp" /> <TextView android:id="@android:id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center|left" android:paddingBottom="4dp" android:paddingTop="4dp" android:textIsSelectable="true" android:textSize="24sp" > </TextView> </LinearLayout> 

Ahora Eclipse me advierte con This tag and its children can be replaced by one <TextView/> and a compound drawable . todo funciona bien, pero odio las advertencias ahora he cambiado la vista.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@android:id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center|left" android:paddingBottom="4dp" android:paddingTop="4dp" android:textIsSelectable="true" android:textSize="24sp" android:drawableLeft="@drawable/unstarred" > </TextView> </LinearLayout> 

pero la imagen no se muestra, puede decirme lo que está mal en el diseño, este es mi adaptador

 public class CategoryAdapter extends ArrayAdapter<Category> { private Context mContext; private int mLayoutResourceId; private Category mCategories[] = null; public CategoryAdapter(Context context, int layoutResourceId, Category[] data) { super(context, layoutResourceId, data); mContext = context; mCategories = data; mLayoutResourceId = layoutResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; Holder holder = null; if(null == row) { row = LayoutInflater.from(mContext).inflate(mLayoutResourceId, parent, false); holder = new Holder(); holder.mIcon = (ImageView)row.findViewById(android.R.id.icon); holder.mTitle = (TextView)row.findViewById(android.R.id.title); row.setTag(holder); } else { holder = (Holder) row.getTag(); } // obviously only one is used at a time // First layout final Category category = mCategories[position]; holder.mTitle.setText(category.getName()); holder.mIcon .setImageResource(category.getStarred() == 1 ? R.drawable.starred : R.drawable.unstarred); // Second layout final Category category = mCategories[position]; final Drawable icon = getResources().getDrawable(category.getStarred() == 1 ? R.drawable.starred : R.drawable.unstarred); holder.mTitle.setText(category.getName()); holder.mTitle.setCompoundDrawables(icon, null, null, null); return row; } } public static class Holder { ImageView mIcon; TextView mTitle; } 

el problema es: la imagen no se muestra

He cambiado setCompoundDrawables para setCompoundDrawablesWithIntrinsicBounds y finalmente funciona

Añadir:

 icon.setBounds(0, 0, your icon width, your icon height); 

antes de:

 holder.mTitle.setCompoundDrawables(icon, null, null, null); 

o:

 holder.mTitle.setCompoundDrawablesWithIntrinsicBounds(category.getStarred() == 1 ? R.drawable.starred : R.drawable.unstarred, null, null, null); 

en lugar.

  • AlertDialog con vista personalizada: redimensionar para ajustar el contenido de la vista
  • ¿Cómo puedo utilizar varios valores para las propiedades de diseño XML de Android?
  • Cómo cambiar el tamaño LinearLayout del código
  • Mostrar diseño cuando se abre el teclado virtual
  • Hacer que un botón Datepicker tenga un aspecto similar al de la aplicación de calendario de Google
  • Simular efecto de clic en el botón transparente
  • Cómo desactivar todos los eventos de clic de un diseño?
  • Cómo hacer aplicaciones hermosas y con estilo como Timely
  • Android - diseño lineal multilínea
  • Eliminar la barra de título predeterminada
  • Android ImageView: Ajustar el ancho
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.