Android – textview setcomposundrawables do not change or display

I have a listview with custom list items

<?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>

Now eclipse warns me that this tag and its descendants can be replaced by a < textview / > and a composite drawable. Everything is normal, but I hate warnings. Now I change my point of view

<?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>

But the image is not displayed. You can tell me what's wrong with the layout. This is my adapter

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;        
}

The problem is: the image is not displayed

resolvent:

I changed setcomposundrawables for setcomposundrawables with intrinsic boundaries, and finally worked

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>