2010-11-25 113 views
4

我试图将我的应用程序背景设置为用户选择的图像,但我遇到了一些麻烦。有人可以给我一个如何做到这一点的例子吗?我可以从我的资源中设置图像,但是当我尝试在手机上使用图像时,我似乎无法使其工作。Android - 设置应用程序背景

回答

3

假设你已经创建了让用户选择的路径图像的方法,使用此:

// Variable with the path to the background 
    String bg_path = "/sdcard/bg/background.png" // <-- This path can be whatever you like 

    //Change background of Activity  
    getWindow().setBackgroundDrawable(Drawable.createFromPath(bg_path)); 

哦,不要忘了布局设置背景颜色为你透明的XML文件中,或者你将无法看到图像。 (例如,对于像列表视图那样填充父窗口的任何东西都是有效的)

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout="@+id/m_layout" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android.background="Color.TRANSPARENT" 
    >