自定义带图标的Preferecnce-----类ListPreference实现(1)

            有时候系统提供给我们的preference并不能满足我们的要求,所以有的时候需要我们自定义Preferece,下面的例子就是我个人自定义的一个简单的带图标的Preference。 

             首先是xml布局文件,就是你想实现的布局。

<?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="18dip"
        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="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dip"
        android:layout_marginRight="6dip"
        android:layout_gravity="center" />
</LinearLayout> 


下面是自定义的preference的java文件:


import com.android.mms.R;

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

public class IconListPreference extends Preference{
private Drawable mIcon;
    
    public IconListPreference(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs);
        
        this.setLayoutResource(R.layout.icon_list_preference);

        //这里设置的是icon初始化的图标
        this.mIcon = context.getResources().getDrawable(R.drawable.ycz20_black);
    }

    public IconListPreference(final Context context, final AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    /**
* Sets the icon for this Preference with a Drawable.
*
* @param icon The icon for this Preference
*/
    public void setIcon(final Drawable icon) {
        if (((icon == null) && (this.mIcon != null)) || ((icon != null) && (!icon.equals(this.mIcon)))) {
            this.mIcon = icon;
            this.notifyChanged();
        }
    }
    public void setIcon(int iconRes) {
        if(R.drawable.ycz20_black!=iconRes){
            this.mIcon = getContext().getResources().getDrawable(iconRes);
            this.notifyChanged();
        }
    }
    /**
* Returns the icon of this Preference.
*
* @return The icon.
* @see #setIcon(Drawable)
*/
    public Drawable getIcon() {
        return this.mIcon;
    }
}


比如你想更改ImageView里面的图标,就是以使用setIcon()方法。

因为自己完成的功能比较简单,所以重写的方法就比较少,大家 可以根据自己的需要来添加更多的方法

而只是实现了一个Preferecnce,但是如何实现一个类似ListPreference类似的功能,但是比ListPreference更绚丽的效果呢?请看下一篇!

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
ListPreferenceAndroid 中的一个 UI 元素,用于在设置中显示一个可选项列表,并允许用户选择其中一个选项。默认情况下,ListPreference 显示一个预定义的选项列表,但是我们可以通过扩展它来自定义选项列表。 首先,我们需要创建一个来扩展 ListPreference,然后覆盖 `onPrepareDialogBuilder()` 方法,在该方法中设置我们自定义的选项列表。下面是一个简单的自定义 ListPreference 的示例代码: ```java public class CustomListPreference extends ListPreference { private Context mContext; private CharSequence[] mEntries; private CharSequence[] mEntryValues; public CustomListPreference(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; } @Override protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { super.onPrepareDialogBuilder(builder); // 设置自定义的选项列表 builder.setAdapter(new CustomListAdapter(mContext, mEntries, mEntryValues), null); } public void setEntries(CharSequence[] entries) { mEntries = entries; } public void setEntryValues(CharSequence[] entryValues) { mEntryValues = entryValues; } private class CustomListAdapter extends ArrayAdapter<CharSequence> { public CustomListAdapter(Context context, CharSequence[] entries, CharSequence[] entryValues) { super(context, R.layout.custom_list_preference_item, R.id.title, entries); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); // 在这里设置选项的样式,比如字体大小、颜色等 TextView titleView = view.findViewById(R.id.title); titleView.setTextSize(18); return view; } } } ``` 在上面的代码中,我们创建了一个名为 CustomListPreference来扩展 ListPreference。在 `onPrepareDialogBuilder()` 方法中,我们使用 `setAdapter()` 方法将自定义的选项列表设置为 AlertDialog 的 Adapter。在 CustomListAdapter 中,我们可以设置选项的样式,比如字体大小、颜色等。注意,在自定义列表项布局文件 custom_list_preference_item.xml 中,我们需要至少包含一个名为 title 的 TextView,以便在 CustomListAdapter 中使用。 最后,我们可以在 XML 文件中使用 CustomListPreference,如下所示: ```xml <com.example.CustomListPreference android:key="list_preference" android:title="List Preference" android:entries="@array/list_preference_entries" android:entryValues="@array/list_preference_entry_values"/> ``` 在上面的 XML 中,我们使用 CustomListPreference 替换了默认的 ListPreference,并使用 `setEntries()` 和 `setEntryValues()` 方法设置我们自定义的选项列表。注意,我们需要在 `res/values/arrays.xml` 文件中定义 `list_preference_entries` 和 `list_preference_entry_values` 数组。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值