我希望我的某些偏好具有圖標,例如設置應用。在Android偏好設置中使用圖標
我想這樣做的一種方法是從設置應用程序複製所有相關的代碼和資源,但它似乎是一些圖標的矯枉過正。
另外我不喜歡在需要設置圖標的每個項目中複製代碼和資源的想法。
有沒有人用更簡單或無資源的方法解決了這個問題?
我希望我的某些偏好具有圖標,例如設置應用。在Android偏好設置中使用圖標
我想這樣做的一種方法是從設置應用程序複製所有相關的代碼和資源,但它似乎是一些圖標的矯枉過正。
另外我不喜歡在需要設置圖標的每個項目中複製代碼和資源的想法。
有沒有人用更簡單或無資源的方法解決了這個問題?
「設置」應用程序使用私人定製PreferenceScreen
子類具有圖標 - IconPreferenceScreen
。它是51行代碼,包括註釋,但它也需要一些自定義屬性。最簡單的選擇是將所有這些克隆到您的項目中,即使您不喜歡它。
更新....回答和工作
使用自定義佈局的圖標偏好
<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的偏好設置,並添加圖標
檢查設置應用程序的attrs.xml。 – hpique 2010-04-15 23:49:11
Android 3.x蜂巢顯示在標準首選項中定義的圖標。
因此,圖標的使用可能取決於Android操作系統版本或屏幕大小。
這取決於Android版本,請參閱我上面的答案。 – keaukraine 2013-01-17 12:52:21
由於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-我建議。
我不明白,爲什麼你不能只使用自定義佈局? http://developer.android.com/reference/android/preference/Preference.html#setLayoutResource%28int%29 – 2011-04-26 17:23:40