图标未显示在ICS +中的首选项

时间:2013-11-05 10:39:36

标签: android layout preferences

我正在使用找到here的IconPreferenceScreen类来创建一个右侧有删除图标的首选项列表。

这是我用过的代码:

protected void updateFavoritesCategory() {
        PreferenceCategory favsCategory = (PreferenceCategory) findPreference("pref_category_favs");
        favsCategory.removeAll();

        List<Favorite> favs = myAppDB.getFavorites();

        for (Favorite f : favs) {

            String stName = StringUtils.unescapeHTML(f.getName());
            stName = URLDecoder.decode(stName);

            IconPreferenceScreen favPreference = new IconPreferenceScreen(this, R.drawable.ic_delete, f.getId().toString());
            favPreference.setIconClickedListener(this);
            favPreference.setTitle(stName);
            favsCategory.addPreference(favPreference);
        }

    }

此代码适用于Gingerbread:

enter image description here

正如您所看到的,它显示了标题,摘要和图标,但在ICS +设备上,我只看到一个空白条目,没有文字,根本没有图标,当我点击每个元素时,我没有得到突出显示的效果:

enter image description here

这是每个列表元素的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+android:id/widget_frame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:gravity="center_vertical"
    android:minHeight="56dp"
    android:padding="10dp"
    android:paddingEnd="?android:attr/scrollbarSize" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="false"
        android:layout_alignParentLeft="false"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:contentDescription="@null"
        android:src="@drawable/ic_delete" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignBottom="@+id/icon"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/icon"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="center_vertical"
            android:singleLine="true"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="12sp" />

    </LinearLayout>

</RelativeLayout>

关于为什么会发生这种情况的任何想法?

PS:为了更快地参考,这里是IconPreferenceScreen类。我修改了一下,为用户点击图标时添加了一个事件监听器:

public class IconPreferenceScreen extends Preference implements OnClickListener {

    private final Drawable mIcon;
    private static String mType;
    private Context mContext;

    private String tag = "";
    private IconClickedListener iconClickedListener;

    public interface IconClickedListener
    {
        public void onClick(String tag);
    }



    public IconPreferenceScreen(Context context, AttributeSet attrs, int iconRes) {
        this(context, attrs, 0, iconRes);
    }

    public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle, int iconRes) {
        super(context, attrs, defStyle);
        mContext = context;
        setLayoutResource(R.layout.preference_icon);
        mIcon = context.getResources().getDrawable(iconRes); 
    }

    public IconPreferenceScreen(Context context, int iconRes, String tag) {
        this(context, null, iconRes);
        mContext = context;
        this.tag = tag;
    }

    @Override
    public void onBindView(View view) {
        super.onBindView(view);

        ImageView imageView = (ImageView) view.findViewById(R.id.icon);
        imageView.setOnClickListener(this);
        if (imageView != null && mIcon != null) {
            imageView.setImageDrawable(mIcon);
        }
    }

    @Override
    public void onClick(View v) {
        iconClickedListener.onClick(tag);
    }

    public IconClickedListener getIconClickedListener() {
        return iconClickedListener;
    }

    public void setIconClickedListener(IconClickedListener iconClickedListener) {
        this.iconClickedListener = iconClickedListener;
    }
}

0 个答案:

没有答案