如何在Android应用程序中更改背景图像?

时间:2011-07-17 14:11:39

标签: 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*

请帮忙,抱歉我的英文

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

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