简体   繁体   中英

Showing image on ListPreference summary

I'm trying to add images on ListPreference and I achieved that but on Android API level 15 or higher it doesn't work. What I'm doing wrong? I have tested this code on 2.2 and 2.3.3 and all works fine! Here is my code.

private void addSummary(Drawable d, Spannable sp) {

    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

    SpannableStringBuilder builder = new SpannableStringBuilder();
    builder.append(Const.IMAGE_ANCHOR);
    builder.append("  ");
    builder.append(sp);
    setTextColor(builder);
    ImageSpan imageSpan = new ImageSpan(d);
    int end = Const.IMAGE_ANCHOR.length();

    builder.setSpan(imageSpan, 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    listPreference.setSummary(builder);

}

Instead of using this approach a cleaner approach would be to declare a layout for the preference object and set the android:layout parameter of the preference with the resource id of your custom layout.

Here is a working code example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM