2011-07-17 94 views
1

我的应用有一个背景,应该在某些事件发生后更改。那么,我该怎么做呢?如何更改Android应用程序中的背景图像?

的main.xml:

<AbsoluteLayout 
android:id="@+id/widget0" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="@drawable/bg"> 

有背景应改为:

if(a<b) *change background to bg2.jpg goes here* 

帮助,请与遗憾,我的英语

+0

强烈不建议使用AbsoluteLayout。 – PravinCG

回答

2

我看你使用绝对布局。所以只需实例化新的AbsoluteLayout对象。

AbsoluteLayout al = (AbsoluteLayout) findViewById(R.id.widget0); 
//This function will change background drawable, so place it where you want. 
al.setBackgroundDrawable(yourDrawableID); 

顺便问一下,你也可以做

al.setBackgroundResource(resourceID); 

与RESOURCEID应该导航到绘制ID和设置为0,如果你想删除的背景。

+0

不赞成使用AbsoluteLayout类型。我该怎么做才能避免这个错误? – Alexander

+1

已弃用它以确保Android应用程序与许多屏幕分辨率兼容。如果您确实想要使用AbsoluteLayout,请确保兼容。为什么你需要AbsoluteLayout?如果您需要将控件放置在特定位置,则可以在RelativeLayout中执行此操作。 –

+0

谢谢,它适用于我。 – Alexander

0

其简单,android:background="@drawable/YourImage"

+0

您应该将代码写入XML文件的布局标记中。 – anilbey

相关问题