Using icons in Android preferences

三世轮回 提交于 2019-12-03 05:13:52

问题


I would like some of my preferences to have icons, like the Settings app.

I guess one way of doing this would be to copy all the relevant code and resources from the Settings app, but it seems like overkill for a couple of icons.

Also I don't like the idea of having to duplicate the code and resources in each project that requires settings icons.

Has anyone solved this already with a simpler or resource-free approach?


回答1:


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




回答2:


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.




回答3:


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




回答4:


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

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



来源:https://stackoverflow.com/questions/2646001/using-icons-in-android-preferences

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