implement android:src=“@drawable/image” programmatically in Android

前端 未结 6 1065
广开言路
广开言路 2021-02-03 18:24

I am trying to set the foreground image on an image button. After some research, I came across this code sample:



        
6条回答
  •  无人及你
    2021-02-03 19:02

    Here's what worked for me in setting the image:src on an ImageButton programmatically** or through code:

    1.Retrieve the image drawable.

    Drawable tempImage = getResources().getDrawable(R.drawable.my_image);
    

    2.Get the view.

    ImageButton tempButton = (ImageButton)findViewById(R.id.my_image_button);
    

    3.Set the image for the view.

    tempButton.setImageDrawable(tempImage);
    

    Hope this works for you too!

提交回复
热议问题