为什么自定义偏好不显示?

时间:2014-01-29 14:24:31

标签: android

我正在尝试实施此自定义偏好https://gist.github.com/JakeWharton/515681 但偏好不会显示: - ((

enter image description here

编辑编辑编辑

事实证明它适用于Gingerbread但不适用于KitKat: - ((<(/

请另外;不是自定义偏好,通常没有问题......


这是 MyPreferencesActivity

public class MyPreferencesActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName("prefs");
addPreferencesFromResource(R.xml.prefs);

// add a validator to the "numberofCircles" preference so that it only
// accepts numbers
Preference circlePreference = getPreferenceScreen().findPreference("numberOfCircles");

// add the validator
circlePreference.setOnPreferenceChangeListener(numberCheckListener);
}
}

这是我的 prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/frankandrobot.glwallpapervideodemo.com" >

<CheckBoxPreference
    android:key="touch"
    android:title="Enable Touch" >
</CheckBoxPreference>

<EditTextPreference
    android:key="numberOfCircles"
    android:title="Number of Circles" >
</EditTextPreference>

<frankandrobot.glwallpapervideodemo.com.IconCheckBoxPreference
        android:key="help"
        android:title="Ciao"
        app:icon="@drawable/icon" />

</PreferenceScreen>

这是自定义偏好设置

package frankandrobot.glwallpapervideodemo.com;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;


public class IconCheckBoxPreference extends CheckBoxPreference {
private Drawable mIcon;

public IconCheckBoxPreference(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);

    this.setLayoutResource(R.layout.icon_checkbox_preference);

    this.mIcon = context.obtainStyledAttributes(attrs, R.styleable.IconPreference, defStyle, 0).getDrawable(R.styleable.IconPreference_icon);
}

public IconCheckBoxPreference(final Context context, final AttributeSet attrs) {
    this(context, attrs, 0);
}

@Override
protected void onBindView(final View view) {
    super.onBindView(view);

    final ImageView imageView = (ImageView)view.findViewById(R.id.icon);
    if ((imageView != null) && (this.mIcon != null)) {
        imageView.setImageDrawable(this.mIcon);
    }
}

/**
 * Sets the icon for this Preference with a Drawable.
 *
 * @param icon The icon for this Preference
 */
public void setIcon(final Drawable icon) {
    if (((icon == null) && (this.mIcon != null)) || ((icon != null) && (!icon.equals(this.mIcon)))) {
        this.mIcon = icon;
        this.notifyChanged();
    }
}

/**
 * Returns the icon of this Preference.
 *
 * @return The icon.
 * @see #setIcon(Drawable)
 */
public Drawable getIcon() {
    return this.mIcon;
}
}

这是我的 attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="IconPreference">
    <attr name="icon" format="reference" />
</declare-styleable>
</resources>

1 个答案:

答案 0 :(得分:1)

最简单的答案是:CheckBoxPreference,至少对于Android 4.1+,支持android:icon,因此您不需要自己的自定义偏好类。

我对4.1进行套期保值的唯一原因是我没有尝试过任何旧版本。