how to replace/change image button programmatically android

≯℡__Kan透↙ 提交于 2020-05-25 04:34:27

问题


I have an image button on my view which i need to change after user interaction. I dont find nothing like myImageButton.setDrawable Here is my xml for the button i want to change:

<ImageButton
        android:id="@+id/stopButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/buttons_background"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:background="@null"
        android:src="@drawable/btn_play" />

The only line I need to change android:src="@drawable/btn_play" to android:src="@drawable/btn_stop" programmatically. Can this be done? Thanks in advance.


回答1:


You can use

setImageResource for the image button



回答2:


Use this:

myImageButton.setImageResource(R.drawable.btn_stop);



回答3:


ImageButton = (ImageButton)findViewById(R.id.stopButton);
btn.setCompoundDrawablesWithIntrinsicBounds(R.drawable.btn_stop, 0, 0, 0);



回答4:


Use

setImageResource(R.drawable.btn_stop);



来源:https://stackoverflow.com/questions/16526112/how-to-replace-change-image-button-programmatically-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!