Preference with icon not showing in ICS+

烂漫一生 提交于 2019-12-11 12:37:02

问题


I am using the IconPreferenceScreen class found here to create a preference list with a delete icon on the right.

This is the code I have used:

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

    }

This code works just fine on Gingerbread:

As you can see it shows the title, summary and the icon, but on ICS+ devices I just see a blank entry, no text, no icon at all and I don't get the highlighted effect when I click on each element:

This is the XML for each list element:

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

Any ideas on why this is happening?

PS: for quicker reference, here's the IconPreferenceScreen class. I modified it a little bit to add an event listener for when user clicks the icon:

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

来源:https://stackoverflow.com/questions/19786999/preference-with-icon-not-showing-in-ics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!