我有一个带有自定义列表项的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
警告我。一切正常,但我讨厌警告,现在我已将视图改为。
<?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;
}
问题是:图片未显示
答案 0 :(得分:23)
我为setCompoundDrawables
更改了setCompoundDrawablesWithIntrinsicBounds
,最后工作
答案 1 :(得分:1)
我知道这是一个老问题,但是相关(至少对我来说;-)),所以也许其他人正在寻找答案。
虽然user4294815的answer非常接近,但没有一个答案实际描述了问题所在。
发生这种情况的原因是因为#setBounds()
未在您的图标上调用,因此除了width = 0
和height = 0
之外,它永远不会有任何大小。
另外,如果您查看#setCompoundDrawables()
的文档,它实际上表明您需要在设置#setBounds()
之前在Drawable
上致电Drawable
:
/** * Sets the Drawables (if any) to appear to the left of, above, to the * right of, and below the text. Use {@code null} if you do not want a * Drawable there. The Drawables must already have had * {@link Drawable#setBounds} called. * <p> * Calling this method will overwrite any Drawables previously set using * {@link #setCompoundDrawablesRelative} or related methods. * * @attr ref android.R.styleable#TextView_drawableLeft * @attr ref android.R.styleable#TextView_drawableTop * @attr ref android.R.styleable#TextView_drawableRight * @attr ref android.R.styleable#TextView_drawableBottom */
现在要解决此问题,您可以在#setBounds()
上致电Drawable
,或者如果您未直接使用Drawable
,则必须使用{{1} }或#setCompoundDrawablesWithIntrinsicBounds()
。后者今天更合适,因为它不使用#setCompoundDrawablesRelativeWithIntrinsicBounds()
和left
namings,而是使用right
和start
,因此图标将相对于阅读设置方向。
我知道rkmax自己的answer回答了这个问题,或者至少提供了解决方案,但有时这还不够 - 在我看来,解释问题的文字更合适,并为下一个看这个问题的人添加说明问题
答案 2 :(得分:0)
添加:
icon.setBounds(0, 0, your icon width, your icon height);
之前:
holder.mTitle.setCompoundDrawables(icon, null, null, null);
或:
holder.mTitle.setCompoundDrawablesWithIntrinsicBounds(category.getStarred() == 1 ? R.drawable.starred : R.drawable.unstarred, null, null, null);
代替。