开发者

Android - Set Application Background

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 wor开发者_StackOverflow中文版k.


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)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜