Android简单小部件将图像显示到ImageView中

Android简单小部件将图像显示到ImageView中,android,android-widget,Android,Android Widget,我创建了一个从ImageView扩展而来的简单小部件,在这个简单的代码中,我想将图像从布局xml设置为ImageView,我想更改背景ImageView图像,并更改图像以将其他图像设置为ImageView,但我第一次遇到的问题是不显示图像 1) 我正在将新属性创建到attr.xml中: <declare-styleable name="CIconButton"> <attr name="button_icon" format="integer" />

我创建了一个从ImageView扩展而来的简单小部件,在这个简单的代码中,我想将图像从布局xml设置为ImageView,我想更改背景ImageView图像,并更改图像以将其他图像设置为ImageView,但我第一次遇到的问题是不显示图像

1) 我正在将新属性创建到attr.xml中:

<declare-styleable name="CIconButton">
    <attr name="button_icon"     format="integer" />
    <attr name="background_icon" format="reference" />
</declare-styleable>
问题:


我的自定义窗口小部件类不将图像集显示到布局xml中,例如,该类不显示可绘制的
图标\u添加\u操作
我的问题已解决,我必须将第二个构造函数更改为:

public CIconButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}
致:


this
调用last this class constructor

如果您正在扩展
ImageView
,为什么不直接使用
android:src
属性,让
super
构造函数负责加载图像?@Mike M.对于在
CIconButton类中获取变量attr
对不起,我不明白你的意思。@Mahdi.Pishguy。你应该使用android:src来显示你的图标Imageview@Ramesh那篇文章与这个问题无关。
public class CIconButton extends ImageView implements OnClickListener {
    private Drawable mSelectedBackground;

    public CIconButton(Context context) {
        super(context, null);
    }
    public CIconButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CIconButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CIconButton, defStyle, 0);
        mSelectedBackground = a.getDrawable(R.styleable.CIconButton_button_icon);
        setImageDrawable(mSelectedBackground);
        a.recycle();
        setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
    }
}
public CIconButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public CIconButton(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}