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

如何在android应用程序中更改背景图像?,android,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="@drawab

我的应用程序中有一个背景,应该在某些事件后更改。那么,我该怎么做呢

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*

if(a我看到您使用了AbsoluteLayout。所以只需实例化新的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);

如果要删除背景,则with resourceID应导航到可绘制ID并设置为0。

很简单,
android:background=“@drawable/YourImage”

强烈建议不要使用AbsoluteLayout。AbsoluteLayout类型已被弃用。我该如何避免此错误?弃用该类型是为了确保Android应用程序与多种屏幕分辨率兼容。如果确实要使用AbsoluteLayout,请确保其兼容。为什么需要AbsoluteLayout?如果需要放置control在特定位置,这是在RelativeLayout中实现的一种方法。您应该在XML文件的布局标记中编写代码。