如何添加图标preference图标、preference

2023-09-12 01:37:01 作者:愿是你心之王

我在做一个应用程序,扩展了preferenceActivity,我想一个图标添加到每个preference。

我看过一个类似的问题,这是与更多的信誉答案:

CommonsWare说:

  

Settings(设置)应用程序使用私人定制preferenceScreen子类有图标 - 图标preferenceScreen。这是51行code,包括评论,虽然它也需要一些自定义属性。最简单的方法是克隆了这一切到您的项目,即使你不喜欢这一点。

但我不能使它发挥作用。现在我克隆类图标preferenceScreen到我的项目。我不知道我必须在这之后的事情。我试图做一个新的图标preferenceScreen我不能使它工作。

 图标preferenceScreen测试=新图标preferenceScreen();
    test.setIcon(图标);
 

解决方案

在许多测试和许多错误,我能得到它!

我不得不这样做:

1 - 克隆从Android原生应用程序的设置(感谢CommonWare)类图标preferenceScreen

2 - 克隆从Android设置应用程序布局文件preference_icon.xml

3 - 申报文件attrs.xml图标preferenceScreen设置样式:

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <申报,设置样式名称=图标preferenceScreen>
         < attr指示NAME =图标格式=参考/>
    < /申报,设置样式>
< /资源>
 
全英文的电脑右下方没有声音快捷图标怎么添加

4 - 声明图标preferenceScreen在preference.xml文件:

 < com.app.example.Icon preferenceScreen
                机器人:标题=图标preferenceScreen标题
                机器人:总结=图标preferenceScreen摘要
                机器人:关键=键1/>
 

5 - 最后定为preference的图标,在preference类:

 添加preferencesFromResource(R.xml.example);
图标preferenceScreen试验=(图标preferenceScreen)找到preference(键1);
资源RES = getResources();
可绘制图标= res.getDrawable(R.drawable.icon1);
test.setIcon(icono1);
 

再次感谢CommonsWare的告诉我从哪里开始,并为他的解释。

这是克隆的图标preferenceScreen类:

 包com.app.example;

进口android.content.Context;
进口android.content.res.TypedArray;
进口android.graphics.drawable.Drawable;
。进口的Andr​​oid preference preference;
进口android.util.AttributeSet;
进口android.view.View;
进口android.widget.ImageView;

公共类图标preferenceScreen扩展preference {

    私人绘制对象米康;

    公共图标preferenceScreen(上下文的背景下,ATTRS的AttributeSet){
        这(背景下,ATTRS,0);
    }

    公共图标preferenceScreen(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        setLayoutResource(R.layout preference_icon。);
        TypedArray A = context.obtainStyledAttributes(ATTRS,
                R.styleable.Icon preferenceScreen,defStyle,0);
        米康= a.getDrawable(R.styleable.Icon preferenceScreen_icon);
    }

    @覆盖
    公共无效onBindView(查看视图){
        super.onBindView(视图);
        ImageView的ImageView的=(ImageView的)view.findViewById(R.id.icon);
        如果(ImageView的= NULL和放大器;!&安培;!米康= NULL){
            imageView.setImageDrawable(米康);
        }
    }

    公共无效的setIcon(可绘制图标){
        如果((图标== NULL和放大器;&安培;!米康= NULL)||(图标= NULL和放大器;&安培;!icon.equals(微型计算机))){
            米康=图标;
            notifyChanged();
        }
    }

    公众可绘制调用getIcon(){
        返回米康;
    }
}
 

这是克隆的preference_icon.xml布局:

 <的LinearLayout机器人:ID =@ +安卓ID /图标preF
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:=了minHeight机器人:ATTR /列表preferredItemHeight
    机器人:重力=center_vertical
    机器人:paddingRight =机器人:ATTR / scrollbarSize>
    < ImageView的机器人:ID =@ + ID /图标
    机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =6dip
        机器人:layout_marginRight =6dip
        机器人:layout_gravity =中心/>
    < RelativeLayout的机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =2DIP
        机器人:layout_marginRight =6dip
        机器人:layout_marginTop =6dip
        机器人:layout_marginBottom =6dip
        机器人:layout_weight =1>
        < TextView的机器人:ID =@ +安卓ID /标题
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:单线=真
            机器人:textAppearance =机器人:ATTR / textAppearanceLarge
            机器人:ellipsize =金字招牌
            机器人:fadingEdge =横向/>
        < TextView的机器人:ID =@ +安卓ID /摘要
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@机器人:ID /标题
            机器人:layout_alignLeft =@机器人:ID /标题
            机器人:textAppearance =机器人:ATTR / textAppearanceSmall
            机器人:MAXLINES =2/>
    < / RelativeLayout的>
< / LinearLayout中>
 

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

CommonsWare Say:

The Settings application uses a private custom PreferenceScreen subclass to have the icon -- IconPreferenceScreen. It is 51 lines of code, including the comments, though it also requires some custom attributes. The simplest option is to clone all of that into your project, even though you do not like that.

But I can't make it work. For now I cloned the class IconPreferenceScreen to my project. And I don't know what I have to do after this. I'm trying to make a new IconPreferenceScreen I can't make it work..

    IconPreferenceScreen test = new IconPreferenceScreen();
    test.setIcon(icon);

解决方案

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>

 
精彩推荐
图片推荐