2010-04-15 50 views
11

我希望我的某些偏好具有圖標,例如設置應用。在Android偏好設置中使用圖標

我想這樣做的一種方法是從設置應用程序複製所有相關的代碼和資源,但它似乎是一些圖標的矯枉過正。

另外我不喜歡在需要設置圖標的每個項目中複製代碼和資源的想法。

有沒有人用更簡單或無資源的方法解決了這個問題?

+0

我不明白,爲什麼你不能只使用自定義佈局? http://developer.android.com/reference/android/preference/Preference.html#setLayoutResource%28int%29 – 2011-04-26 17:23:40

回答

9

「設置」應用程序使用私人定製PreferenceScreen子類具有圖標 - IconPreferenceScreen。它是51行代碼,包括註釋,但它也需要一些自定義屬性。最簡單的選擇是將所有這些克隆到您的項目中,即使您不喜歡它。

source code

+0

如果只有資源可以在項目之間共享。我希望有人已經解決了這個問題,並將其包裝在一個不需要外部資源的好班級中。 – hpique 2010-04-15 14:30:20

+0

應該注意的是,Settings應用程序方法擴展了Preference,而不是PreferenceScreen,並且它還需要自定義佈局才能工作。 – hpique 2010-04-16 01:41:34

+0

鏈接的源代碼給出了一個404 – Shine 2012-08-30 14:50:41

5

更新....回答和工作

使用自定義佈局的圖標偏好

<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的偏好設置,並添加圖標

+1

檢查設置應用程序的attrs.xml。 – hpique 2010-04-15 23:49:11

1

Android 3.x蜂巢顯示在標準首選項中定義的圖標。

因此,圖標的使用可能取決於Android操作系統版本或屏幕大小。

+0

這取決於Android版本,請參閱我上面的答案。 – keaukraine 2013-01-17 12:52:21