2013-07-22 32 views
1

因此,我正在使用Android 4.0庫的Android應用程序。從Java類中更改RelativeLayout的背景圖像(Android應用程序)

此應用程序的活動之一是由一個具有圖像背景和切換按鈕的RelativeLayout組成。 當用戶切換按鈕時,佈局的背景圖像必須更改。

所以它必須從activity.java類的內部改變:

if (toggleButton.isChecked()){ 

// Change the background of the activity to image 2 (for example)   
} 

else{ // when toggle button is off 

// Change it back to image 1 

} 

請幫助我。謝謝:)

回答

1

您使用的方法,setBackgroundView類:

if (toggleButton.isChecked()){ 

// Change the background of the activity to image 2 (for example) 
View myView = this.findViewById(yourViewId); 
myView.setBackgroundResource(yourImage);   
} 

else{ // when toggle button is off 

// Change it back to image 1 
// Change the background of the activity to image 2 (for example) 
View myView = this.findViewById(yourViewId); 
myView.setBackgroundResource(yourOtherImage); 
} 
+0

謝謝你,這是一種有益的。但是,它並沒有像那樣工作。不知何故,該活動拒絕使用setBackground()方法。 – HudaOmais

+1

我用下面的: 整數[]圖像= { \t \t \t R.drawable.image0, \t \t \t R.drawable.image1 \t}; 然後: 如果(toggle.isChecked()){ \t \t myView.setBackgroundResource(圖像[0]); \t } else { \t myView.setBackgroundResource(images [1]); \t } 這解決了我:) – HudaOmais

+0

我編輯了我的答案。很高興我能幫上忙。 – BlackHatSamurai