简体   繁体   English

如何以编程方式替换/更改图像按钮android

[英]how to replace/change image button programmatically android

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: 我没有找到像myImageButton.setDrawable这样的我想要更改的按钮的xml:

<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. 我需要以android:src="@drawable/btn_play"android:src="@drawable/btn_stop"更改为android:src="@drawable/btn_play"android:src="@drawable/btn_stop" Can this be done? 可以这样做吗? Thanks in advance. 提前致谢。

您可以使用

setImageResource for the image button

用这个:

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

使用

setImageResource(R.drawable.btn_stop);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM