java Android - 设置应用程序背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4279077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:39:43  来源:igfitidea点击:

Android - Set Application Background

javaandroidimagebitmap

提问by PeterW

I am trying to set my applications background to a user chosen image, but I am having some trouble. Is it possible for someone to give me an example of how to do this? I can set the image ok from my resources, but when I try to use an image on the phone I can't seem to make it work.

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

回答by Tivie

Assuming you have created a method to let the user choose the path to the image, use this:

假设您已经创建了一个方法让用户选择图像的路径,请使用以下命令:

    // 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));

Oh Don't forget to set you Layout background color to transparent in the XML file or you won't see the image. (this is valid to anything that fills parent window like a listview, for instance)

哦不要忘记在 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"
    >