How to add icons to Preference

后端 未结 9 889
一生所求
一生所求 2020-12-04 18:19

I\'m making an app that extends the PreferenceActivity and I want to add an icon to each Preference.

I read a similar question, and this is the answer with more repu

相关标签:
9条回答
  • 2020-12-04 19:05

    The best and easiest way to achieve what you want is to make the icon a 9-patch icon with the right border as the stretchable area.

    Let's say you have an EditTextPreference that you want to add an icon before the title and summary.

    You create a MyEditTextPreference class that extends EditTextPreference and override the getView method to add your 9-patch icon as the background resource.

    Here is a sample code that works:

    public class MyEditTextPreference extends EditTextPreference {
    
        public MyEditTextPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public View getView(View convertView, ViewGroup parent) {
            View view = super.getView(convertView, parent);
            view.setBackgroundResource(R.drawable.my_icon);
            return view;
        }
    }
    

    Since the icon is a 9-patch, the icon will stretch the transparent part until the right end of the cell, placing the icon on the left side.

    This is a very clean solution for your problem.

    0 讨论(0)
  • 2020-12-04 19:05

    Try this:

    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
    PreferenceScreen ps= getPreferenceManager().createPreferenceScreen(this);
    ps.setKey("ps");
    ps.setLayoutResource(R.layout.your_layout);
    root.addPreference(ps);
    
    0 讨论(0)
  • 2020-12-04 19:06

    After many tests and many mistakes I could get it!

    I had to do this:

    1 - Clone the class IconPreferenceScreen from the Android native Settings app (thanks CommonWare)

    2 - Clone the layout file preference_icon.xml from the Android Settings app.

    3 - Declare the IconPreferenceScreen styleable in the file attrs.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="IconPreferenceScreen">
             <attr name="icon" format="reference" />
        </declare-styleable>
    </resources>
    

    4 - Declare the IconPreferenceScreen in the preference.xml file:

    <com.app.example.IconPreferenceScreen 
                    android:title="IconPreferenceScreen Title"
                    android:summary="IconPreferenceScreen Summary"
                    android:key="key1" />
    

    5 - Finally set the icon for the preference, in the preference class:

    addPreferencesFromResource(R.xml.example);
    IconPreferenceScreen test = (IconPreferenceScreen) findPreference("key1");
    Resources res = getResources();
    Drawable icon = res.getDrawable(R.drawable.icon1);
    test.setIcon(icono1);
    

    Thanks again to CommonsWare for tell me where to start, and for his explanation.

    This is the cloned IconPreferenceScreen class:

    package com.app.example;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.drawable.Drawable;
    import android.preference.Preference;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.ImageView;
    
    public class IconPreferenceScreen extends Preference {
    
        private Drawable mIcon;
    
        public IconPreferenceScreen(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setLayoutResource(R.layout.preference_icon);
            TypedArray a = context.obtainStyledAttributes(attrs,
                    R.styleable.IconPreferenceScreen, defStyle, 0);
            mIcon = a.getDrawable(R.styleable.IconPreferenceScreen_icon);
        }
    
        @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);
            }
        }
    
        public void setIcon(Drawable icon) {
            if ((icon == null && mIcon != null) || (icon != null && !icon.equals(mIcon))) {
                mIcon = icon;
                notifyChanged();
            }
        }
    
        public Drawable getIcon() {
            return mIcon;
        }
    }
    

    And this is the cloned preference_icon.xml layout:

    <LinearLayout android:id="@+android:id/iconpref"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_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>
    
    0 讨论(0)
提交回复
热议问题