所以我正在使用Android 4.0库开发一个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

}


请帮我解决一下这个。谢谢 :)

最佳答案

您使用setBackground类中的View方法:

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);
}

10-07 19:33
查看更多