草庐IT

android - Textview setCompoundDrawables 不改变或显示

coder 2023-12-25 原文

我有一个带有自定义列表项的 ListView

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

现在 Eclipse 使用 This tag and its children can be replaced by one <TextView/> and a compound drawable 警告我.一切正常,但我讨厌警告,现在我已将 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>

但是图像没有显示,可以告诉我布局有什么问题,这是我的适配器

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

问题是:图像没有显示

最佳答案

我将 setCompoundDrawables 更改为 setCompoundDrawablesWithIntrinsicBounds 并最终起作用

关于android - Textview setCompoundDrawables 不改变或显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15709648/

有关android - Textview setCompoundDrawables 不改变或显示的更多相关文章

随机推荐