Using icons in Android preferences

与世无争的帅哥 提交于 2019-12-02 18:29:58
CommonsWare

The Settings application uses a private custom PreferenceScreen subclass to have the icon -- IconPreferenceScreen. It is 51 lines of code, including the comments, though it also requires some custom attributes. The simplest option is to clone all of that into your project, even though you do not like that.

source code

Since API level 11 you can add icon to preferences:

http://developer.android.com/reference/android/preference/Preference.html#setIcon%28int%29

http://developer.android.com/reference/android/preference/Preference.html#attr_android:icon

For older Android versions, you have to use custom layouts and bind images in code, as @roger-l suggests.

Updated.... answered and working

Use a custom layout for the icon preference

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+android:id/widget_frame"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:minHeight="?android:attr/listPreferredItemHeight"
     android:gravity="center_vertical"
     android:paddingRight="?android:attr/scrollbarSize">
     <ImageView
         android:id="@+id/icon"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="6dip"
         android:layout_marginRight="6dip"
         android:layout_gravity="center" />
     <RelativeLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="2dip"
         android:layout_marginRight="6dip"
         android:layout_marginTop="6dip"
         android:layout_marginBottom="6dip"
         android:layout_weight="1">
         <TextView android:id="@+android:id/title"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:singleLine="true"
             android:textAppearance="?android:attr/textAppearanceLarge"
             android:ellipsize="marquee"
             android:fadingEdge="horizontal" />
         <TextView android:id="@+android:id/summary"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_below="@android:id/title"
             android:layout_alignLeft="@android:id/title"
             android:textAppearance="?android:attr/textAppearanceSmall"
             android:maxLines="2" />
     </RelativeLayout>
</LinearLayout>

and the imported class for IconPreferenceScreen

public class IconPreferenceScreen extends Preference {
    private final Drawable mIcon;
    private static String mType;

    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);
        setLayoutResource(R.layout.preference_icon);
        mIcon = context.getResources().getDrawable(iconRes);
    }
    public IconPreferenceScreen(Context context, int iconRes) {
        this(context, null, iconRes);
    }
    @Override
    public void onBindView(View view) {
        super.onBindView(view);
        ImageView imageView = (ImageView) view.findViewById(R.id.icon);
        if (imageView != null && mIcon != null) {
            imageView.setImageDrawable(mIcon);
        }
    }
}

then you can just use a new IconPreferenceScreen in place of a Preference, and add an icon

Android 3.x Honeycomb shows icons defined in standard Preferences.

So probably the use of icon depends on Android OS version or screen size.

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