Java 如何使用代码设置活动的背景?

Java 如何使用代码设置活动的背景?,java,android,eclipse,android-activity,Java,Android,Eclipse,Android Activity,我目前正在使用XML布局来设置我的活动背景。如何在Java代码中实现这一点 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" andr

我目前正在使用XML布局来设置我的活动背景。如何在Java代码中实现这一点

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/bg" >


在调用之前,请使用主
视图上的方法之一
将id属性设置为父RelativeLayout。然后在活动代码中按id查找它,并使用setBackgroundResource方法。

我的活动类中没有看到该函数。如果在setContent()之后,它与android:layout\u height=“fill\u parent”提交了一个相对视图,为什么要设置背景呢?不,我的意思是我没有看到setBackground()。再次编辑。基类视图有3个方法变体。虽然这相当于您在上面使用的代码,但这两种方法都会导致额外的透支级别,从而影响应用程序的绘图性能。设置活动背景的最佳方法是使用自定义主题将windowBackground设置为可绘制。更多信息在这里:如果你在蜂巢或更高版本上,确保你是基于Theme.Holo family,在支持它的设备上获得现代UI。