安卓 - 设置应用程序背景

4

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

1个回答

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文件中将布局背景颜色设置为透明,否则您将看不到图像。(对于像ListView这样填充父窗口的任何内容,这也是有效的)

<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"
    >

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接