【问题标题】:Using icons in Android preferences在 Android 偏好设置中使用图标
【发布时间】:2011-02-08 09:56:53
【问题描述】:

我希望我的某些偏好有图标,例如“设置”应用。

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

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

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

【问题讨论】:

标签: android resources settings


【解决方案1】:

从 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 建议的那样。

【讨论】:

  • 它需要的图标分辨率是多少?
  • 我使用了 32x32 dip
  • 有一些填充?或者内容的大小正好是这个尺寸?
【解决方案2】:

Android 3.x Honeycomb 显示标准首选项中定义的图标。

因此,图标的使用可能取决于 Android 操作系统版本或屏幕尺寸。

【讨论】:

  • 取决于安卓版本,见我上面的回答。
【解决方案3】:

设置应用程序使用私有自定义 PreferenceScreen 子类来拥有图标 -- IconPreferenceScreen。它是 51 行代码,包括 cmets,尽管它还需要一些自定义属性。最简单的选择是将所有这些克隆到您的项目中,即使您不喜欢这样做。

source code

【讨论】:

  • 如果只有资源可以在项目之间共享。我有点希望有人已经解决了这个问题,并将它包装在一个没有任何外部资源的好类中。
  • 应该注意,设置应用程序方法扩展了 Preference,而不是 PreferenceScreen,并且它还需要自定义布局才能工作。
  • @Shine:我一开始没有把链接放在那里。 androidxref.com/source/xref/packages/apps/Settings/src/com/…
【解决方案4】:

已更新....已回答并正常工作

为图标偏好使用自定义布局

<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>

以及 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);
        }
    }
}

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

【讨论】:

  • 签入设置应用的attrs.xml。
猜你喜欢
  • 1970-01-01
  • 2015-06-08
  • 2013-03-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多