android - Honeycomb/ICS 中的自定义首选项被破坏

标签 android android-layout android-3.0-honeycomb android-preferences android-4.0-ice-cream-sandwich

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

http://imgur.com/vKPOu

http://imgur.com/EiMBr

正在为首选项留出空间,但即使是默认标题/摘要也没有显示,而且项目选择的 Intent 也没有触发。这是首选项布局:

<?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 上遇到过问题。有什么建议吗?

最佳答案

我在第三方自定义首选项中遇到了类似的问题,并通过使小部件框架可见来修复它。它在 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);

还必须重新对齐 View 以使其与其余的 ICS 控件保持一致。

关于android - Honeycomb/ICS 中的自定义首选项被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8762984/

相关文章:

java - "ScrollView can only host 1 child"在 2 个 fragment 中,只有 1 个 child ?

android - 发布应用程序的内容存储

android - CardView 示例不工作

android - ViewPager 或 ScrollView 中的 WebView - Android 3.0+ 上的奇怪渲染错误

android - 如何获取上一个或下一个 View

Android Studio 未运行显示 "force close"的应用程序

java - 打开软键盘时顶部粘性标题 android

Android 列表项布局

Android 3.x/HLS 如何在流结束时开始

android - 如何从 AsyncTask Android 获取字符串