我正在尝试将我的应用程序背景设置为用户选择的图像,但我遇到了一些麻烦。有人可以给我一个如何做到这一点的例子吗?我可以从我的资源中设置图片,但当我尝试在手机上使用图片时,我似乎无法让它工作。
最佳答案
假设您已经创建了一个让用户选择图像路径的方法,请使用:
// 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"
>
关于java - Android - 设置应用程序背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4279077/