Imagebutton change programmatically?

前端 未结 5 1963
天命终不由人
天命终不由人 2021-02-11 12:21

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

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

         


        
5条回答
  •  误落风尘
    2021-02-11 12:30

    Using Kotlin, you can do this:

    val myImageButton = ImageButton(context).apply({
        background = null
        setImageDrawable(ContextCompat.getDrawable(context, 
                             R.drawable.ic_save_black_24px))
    })
    

提交回复
热议问题