在Honeycomb/ICS中自定义偏好设置失效

4

我正在使用自定义首选项,其中包含标题、摘要和图标。该首选项用于选择一个项目(皮肤、应用程序等),然后它将总结当前的选择。这是首选项在正常工作时的情况(顶部),以及我在Honeycomb/ICS上遇到的问题:

http://imgur.com/vKPOu

http://imgur.com/EiMBr

为偏好设置腾出空间,但即使使用默认的标题/摘要也没有显示出来,选择项目的意图也没有触发。这是偏好设置的布局:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dip"
        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>
   <ImageView
       android:id="@+id/icon"
       android:layout_width="48dp"
       android:layout_height="48dp"
       android:layout_gravity="center" />
</LinearLayout> 

还有自定义首选项本身:

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class SelectedAppPreference extends Preference {
    private Drawable mIcon;

    public SelectedAppPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        final PackageManager pm = context.getPackageManager();
        String packageName = context.getSharedPreferences("appPrefs", Context.MODE_PRIVATE).getString("selected_app_package", null);

        this.setLayoutResource(R.layout.icon_pref);
        try {
            this.mIcon = pm.getApplicationIcon(packageName);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public SelectedAppPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        final PackageManager pm = context.getPackageManager();
        String packageName = context.getSharedPreferences("appPrefs", Context.MODE_PRIVATE).getString("selected_app_package", null);

        this.setLayoutResource(R.layout.icon_pref);
        try {
            this.mIcon = pm.getApplicationIcon(packageName);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @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);
        }
    }

    public void setIcon(final Drawable icon) {
        if (((icon == null) && (this.mIcon != null)) || ((icon != null) && (!icon.equals(this.mIcon)))) {
            this.mIcon = icon;
            this.notifyChanged();
        }
    }

    public Drawable getIcon() {
        return this.mIcon;
    }
}

偏好设置本身并没有什么特别的。我有一种感觉,问题在于偏好设置布局xml文件中,但我不确定哪里出了问题。我可以确认,在Android 2.1、2.2和2.3设备上,偏好设置正常工作,而在3.2和4.0上存在问题。有什么建议吗?

2个回答

6

我曾遇到一个与第三方自定义设置相关的类似问题,通过将小部件框架可见化解决了该问题。在ICS中,默认情况下它是不可见的。不知道它如何映射到你的自定义设置。

// This line was in the original code.
LinearLayout widgetFrameView = ((LinearLayout) mView
                    .findViewById(android.R.id.widget_frame));
...
// This line fixed the visibility issue
widgetFrameView.setVisibility(View.VISIBLE);

我还需要重新调整视图,使其与其他ICS控件保持一致。


很好,那就是问题所在。你记得你改了什么来重新对齐视图吗? - nsiderTalon
1
谷歌,为什么要做出这样的破坏性更改? - Nappy

2

我修复的第三方自定义偏好设置来自于此网站

https://github.com/attenzione/android-ColorPickerPreference/tree/master/src/net/margaritov/preference/colorpicker

我的代码(位于ColorPickerPreference.setPreviewColor()中)如下所示

widgetFrameView.setVisibility(View.VISIBLE);
final boolean preApi14 = android.os.Build.VERSION.SDK_INT < 14;
final int rightPaddingDip = preApi14 ? 8 : 5;

widgetFrameView.setPadding(
                      widgetFrameView.getPaddingLeft(),
                      widgetFrameView.getPaddingTop(),
                      (int)(mDensity * rightPaddingDip),
                      widgetFrameView.getPaddingBottom()
                );

在哪里

float mDensity = getContext().getResources().getDisplayMetrics().density;

这是使用minSdkVersion = 8和没有targetSdkVersion的情况。如果您将targetSdkVersion设置为14或更高,则可能需要将“5”更改为其他内容,直到右侧的自定义首选项元素与标准元素(例如复选框)对齐。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接