在Android偏好设置中使用图标

hpi*_*que 11 settings resources android

我希望我的一些偏好设置图标,例如"设置"应用.

我想这样做的一种方法是从"设置"应用中复制所有相关的代码和资源,但对于几个图标来说似乎有些过分.

此外,我不喜欢在每个需要设置图标的项目中复制代码和资源的想法.

有没有人用更简单或无资源的方法解决这个问题?

Com*_*are 9

"设置"应用程序使用私有自定义PreferenceScreen子类来显示图标 - IconPreferenceScreen.这是51行代码,包括注释,但它也需要一些自定义属性.最简单的选择是将所有这些克隆到项目中,即使您不喜欢它.

源代码


kea*_*ine 7

从API级别11开始,您可以向首选项添加图标:

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

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

对于较旧的Android版本,您必须使用自定义布局并在代码中绑定图像,如@ roger-l所示.


rog*_*r l 5

更新....回答和工作

使用图标首选项的自定义布局

<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>
Run Code Online (Sandbox Code Playgroud)

和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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后您可以使用新的IconPreferenceScreen代替Preference,并添加一个图标