2013-05-13 106 views
8

我在視圖中有一個圖像按鈕,我需要在用戶交互後進行更改。我不覺得沒有像myImageButton.setDrawable這裏是我的,我想更改按鈕的xml:如何以編程方式替換/更改圖像按鈕android

<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" /> 

我需要改變android:src="@drawable/btn_play"android:src="@drawable/btn_stop"編程的唯一線路。這可以做到嗎?提前致謝。

回答

10

您可以使用

setImageResource for the image button 
+0

非常感謝您的幫助 – madcoderz 2013-05-13 18:37:18

8

使用此:

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

謝謝!適用於按鈕。 – DmitryKanunnikoff 2017-05-13 22:06:06

2

使用

setImageResource(R.drawable.btn_stop);

相關問題