将应用程序背景设置为与主屏幕壁纸相同

时间:2014-07-26 13:31:07

标签: android background wallpaper

我想将我的应用程序的背景设置为与我的主屏幕的壁纸相同。如何在activity.xml中获取主屏幕壁纸?我能这样做吗?

2 个答案:

答案 0 :(得分:13)

使用

final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();

获取当前壁纸。然后将其设置为您自己的应用程序的背景:

LinearLayout ll = (LinearLayout) findViewById(R.id.myLinearLayout);//Substitute with your layout
ll.setBackground(wallpaperDrawable); 

如果你希望将它作为初始背景,那么所有这些都应该在onCreate()中发生。

答案 1 :(得分:7)

虽然@Robin的回答确实将活动的背景设置为主屏幕的壁纸,但它会立即显示整个可绘制的内容,这在大多数情况下会扭曲它(水平压缩它)。

根据您的确切需求,可能更好或更差的其他解决方案是将您的活动主题设置为继承自Theme.Wallpaper的主题。我发现这有点容易,系统会自动显示适当的壁纸切片,这就是我想要的和期望的。

RES / styles.xml:

<style name="wallpaper_theme" parent="@android:style/Theme.Wallpaper">
</style>

的AndroidManifest.xml:

<activity
    ...
    android:theme="@style/wallpaper_theme"
/>