How to set the background screen as Wallpaper all the time?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 10:58:53

问题


Currently, I'm using this to show my application background as phone wallpaper.

getWindow().setFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER,
                     WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);

But for some reason when I start my application by pressing the icon. It just shows the activity screen with the icons on the home screen. I didn't use dialog but it looks like a dialog because layout is just set that way. So I just want to show the wallpaper whenever this activity is running. But it only shows the wallpaper only after the next event occurs such as switching to different activity. I already put that code on onCreate() and whenever I do setContentView()..... Is there way to do such thing or there is just no way?


回答1:


For users of AppCompat, just use the following in your styles.xml, no need for code:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowShowWallpaper">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>



回答2:


After long search and trial and error. I've found the solution to what I wanted. It was just creating separate themes.xml file and just tweak the Theme.Dialog which is already defined in default android themes.xml. All I did was change the Animation part. Originally in android themes.xml the line looks like this.

<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>

but since modifying in android themes.xml doesn't take the effect. I just created my own themes.xml as I said above and just set parent as android:Theme.Dialog. And added a line like this.

<item name="android:windowAnimationStyle">@android:style/Animation</item>

Thanks for the help and I hope this solution helps others.




回答3:


Use following code -

    rl = (RelativeLayout)findViewById(R.id.someid);
    //relative layout is my root node in main.xml (yours may be linearlayout)
    WallpaperManager wm = WallpaperManager.getInstance(this);
    Drawable d = wm.peekDrawable();
    rl.setBackgroundDrawable(d);// You can also use rl.setBackgroundDrawable(getWallpaper);


来源:https://stackoverflow.com/questions/8062286/how-to-set-the-background-screen-as-wallpaper-all-the-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!