vir*_*rho 9 android background wallpaper
我想将我的应用程序的背景设置为与我的主屏幕的壁纸相同.如何在activity.xml中获取主屏幕壁纸?我能这样做吗?
Rob*_*ann 13
使用
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
Run Code Online (Sandbox Code Playgroud)
获得当前的壁纸.然后将其设置为您自己的应用程序的背景:
LinearLayout ll = (LinearLayout) findViewById(R.id.myLinearLayout);//Substitute with your layout
ll.setBackground(wallpaperDrawable);
Run Code Online (Sandbox Code Playgroud)
如果您希望将它作为初始背景,那么所有这些都应该发生在onCreate()中.
虽然@ Robin的答案确实将Activity的背景设置为主屏幕的壁纸,但它会立即显示整个drawable,这在大多数情况下会扭曲它(水平压缩它).
根据您的确切需求,可能更好或更差的不同解决方案是将Activity的主题设置为继承的主题Theme.Wallpaper.我发现这有点容易,系统会自动向您显示适当的壁纸切片,这是我想要的和期望的.
RES/styles.xml:
<style name="wallpaper_theme" parent="@android:style/Theme.Wallpaper">
</style>
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中:
<activity
...
android:theme="@style/wallpaper_theme"
/>
Run Code Online (Sandbox Code Playgroud)