经过多次尝试和错误,我终于做到了!
我必须执行以下步骤:
1 - 从Android本机设置应用程序中克隆IconPreferenceScreen类(感谢CommonWare)。
2 - 克隆Android设置应用程序中的布局文件preference_icon.xml。
3 - 在attrs.xml文件中声明IconPreferenceScreen样式属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="IconPreferenceScreen">
<attr name="icon" format="reference" />
</declare-styleable>
</resources>
4 - 在 preference.xml 文件中声明 IconPreferenceScreen:
<com.app.example.IconPreferenceScreen
android:title="IconPreferenceScreen Title"
android:summary="IconPreferenceScreen Summary"
android:key="key1" />
5 - 最后在首选项类中设置首选项的图标:
addPreferencesFromResource(R.xml.example)
IconPreferenceScreen test = (IconPreferenceScreen) findPreference("key1")
Resources res = getResources()
Drawable icon = res.getDrawable(R.drawable.icon1)
test.setIcon(icono1)
再次感谢CommonsWare告诉我从哪里开始,并对他的解释表示感谢。
这是克隆的IconPreferenceScreen类:
package com.app.example;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
public class IconPreferenceScreen extends Preference {
private Drawable mIcon;
public IconPreferenceScreen(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLayoutResource(R.layout.preference_icon);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.IconPreferenceScreen, defStyle, 0);
mIcon = a.getDrawable(R.styleable.IconPreferenceScreen_icon);
}
@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);
}
}
public void setIcon(Drawable icon) {
if ((icon == null && mIcon != null) || (icon != null && !icon.equals(mIcon))) {
mIcon = icon;
notifyChanged();
}
}
public Drawable getIcon() {
return mIcon;
}
}
这是克隆的 preference_icon.xml 布局:
<LinearLayout android:id="@+android:id/iconpref"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_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>