Imagebutton以编程方式更改? [英] Imagebutton change programmatically?

查看:57
本文介绍了Imagebutton以编程方式更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式更改ImageButton的图像.

I'm trying to change the image of the ImageButton programmatically.

我正在尝试复制此代码,但是setBackgroundDrawable已被弃用.

I'm trying to copy this code, but the setBackgroundDrawable is already deprecated.

public void giveClue(View view) {
    Drawable replacer = getResources().getDrawable(R.drawable.icon2);
    ((ImageButton) view).setEnabled(false);
    ((ImageButton) view).setBackgroundDrawable(replacer);
    gameAdapter.giveClue(game);
}

我的按钮是使用xml创建的,如下所示:

My button was created using xml as follows:

   <ImageButton
        android:id="@+id/ImageButton2"
        android:layout_width="24dp"
        android:layout_height="22dp"
        android:layout_alignTop="@+id/imageButton1"
        android:layout_toLeftOf="@+id/ImageButton3"
        android:src="@drawable/icon" 
        android:onClick="giveClue"/>

推荐答案

您的代码正在尝试更改按钮的背景.不是它的形象. 那是两件事

your code is trying to change the background of the button. not its image. Those are two different things

  ((ImageButton) view).setImageResource(R.drawable.icon2);

这篇关于Imagebutton以编程方式更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆