Imagebutton change programmatically?

前端 未结 4 875
没有蜡笔的小新
没有蜡笔的小新 2021-02-11 12:08

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

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

         


        
4条回答
  •  滥情空心
    2021-02-11 12:26

    Hi you can use the following code

    if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ) 
    {
        ((ImageButton) view).setImageResource(getResources().getIdentifier("icon2", "drawable", getPackageName()));
    }
    else 
    {
        ((ImageButton) view).setImageDrawable(getDrawable(getResources().getIdentifier("icon2", "drawable", getPackageName())));
    }
    

    Hope this will help you.

提交回复
热议问题