将设备壁纸设置为 activity 布局的背景

Set device wallpaper to be the background for an activity layout

我似乎没有明白我的代码有什么问题,将设备壁纸设置为启动器布局的背景 activity。我试图在线搜索此代码以获得更好的代码,但到目前为止我得到的是导致错误的代码:


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

MyLogin = (RelativeLayout) findViewById(R.id.login);
MyLogin.setBackground(wallpaperDrawable);

非常感谢您对此问题的帮助和关注

您可以通过两种方式完成...

  1. 转到该相对布局并在 <RelativeLayout > 中键入 android:background="@drawable/yourImage" for ex

  2. Give the Backround programatically like MyLogin = (RelativeLayout) findViewById(R.id.login); Mylogin.setBackgroundResource(R.drawable.YOUR_IMAGE);

您还可以使用样式将设备壁纸用作Activity背景。 使用

<activity android:theme="@android:style/Theme.Wallpaper.NoTitleBar">

<activity android:theme="@android:style/Theme.Holo.Wallpaper.NoTitleBar">

就您的代码而言,由于您没有指定您遇到的错误,最有可能的是您的 [=22] 中没有 ID 为 loginRelativeLayout =]布局xml

试试这个。它肯定会起作用

        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 
        final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
        MyLogin.post(new Runnable() {

            @Override
            public void run() {
                MyLogin.setBackground(wallpaperDrawable);
            }
        });

您确实应该使用 ActionBar 的后代。此外,您可以选择稍后隐藏操作栏。像这样

<style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

这就是最终对我有用的方法

@SuppressLint("NewApi") @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    gridView = (GridView) findViewById(R.id.gridView1);

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

    MyLogin = (RelativeLayout) findViewById(R.id.login);
    MySong.setBackgroundDrawable(wallpaperDrawable);
    }

错误是当我 运行 应用程序时它只是说应用程序 MyLogin com.example.login 已意外停止。请重试。

但在我将 setBackground 更改为 setBackgroundDrawable 后,它现在可以工作了。

您可以尝试使用

  MyLogin.setBackgroundDrawable(wallpaperDrawable);

而不是

MyLogin.setBackground(wallpaperDrawable);

尽管两者都可以工作,具体取决于设备。

最好的方法是为您制作系统显示壁纸背景。 有 FLAG_SHOW_WALLPAPER and windowShowWallpaper 属性。

可以在此处找到更多示例: