Android:为RelativeLayout设置背景图片?

发布于 2024-10-15 21:52:31 字数 65 浏览 4 评论 0原文

我在运行时下载了图像。现在我想将其设置为 RelativeLayout 的背景。有可能吗?

I have downloaded an image at runtime. Now I want to set it as a background for RelativeLayout. Does it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵你的手,一向走下去 2024-10-22 21:52:31

查看 setBackgroundDrawable ,或者createFromPath在 Drawable 类中。

   RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout);
   Resources res = getResources(); //resource handle
   Drawable drawable = res.getDrawable(R.drawable.newImage); //new Image that was added to the res folder

    rLayout.setBackground(drawable);

Check out the setBackgroundDrawable, or maybe createFromPath in the Drawable class.

   RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout);
   Resources res = getResources(); //resource handle
   Drawable drawable = res.getDrawable(R.drawable.newImage); //new Image that was added to the res folder

    rLayout.setBackground(drawable);
燕归巢 2024-10-22 21:52:31

而是使用:

View lay = (View) findViewById(R.id.rLayout);
lay.setBackgroundResource(R.drawable.newImage);

这有效,因为 R.drawable.newImage 引用一个整数。所以你可以这样做:

int pic = R.drawable.newImage;
lay.setBackgroundResource(pic);

Instead use:

View lay = (View) findViewById(R.id.rLayout);
lay.setBackgroundResource(R.drawable.newImage);

This works because R.drawable.newImage refers to an integer. So you could do:

int pic = R.drawable.newImage;
lay.setBackgroundResource(pic);
眼泪都笑了 2024-10-22 21:52:31

尝试 Xamarin.Android(跨平台) -

RelativeLayout relativeLayout = new RelativeLayout (this);

OR

RelativeLayout relativeLayout = (RelativeLayout)FindViewById (Resource.Id.relativeLayout);

AND

relativeLayout.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.imageName));

Try this for Xamarin.Android (Cross Platform) -

RelativeLayout relativeLayout = new RelativeLayout (this);

OR

RelativeLayout relativeLayout = (RelativeLayout)FindViewById (Resource.Id.relativeLayout);

AND

relativeLayout.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.imageName));
无悔心 2024-10-22 21:52:31

在onCreate函数中:

RelativeLayout baseLayout = (RelativeLayout) this.findViewById(R.id.the_layout_id);

Drawable drawable = loadImageFromAsset();

if(drawable != null){
    baseLayout.setBackground(drawable);
    Log.d("TheActivity", "Setting the background");
}

图片加载方法:

public Drawable loadImageFromAsset() {

    Drawable drawable;

    // load image
    try {

        // get input stream
        InputStream ims = getAssets().open("images/test.9.png");

        //Note: Images can be in hierarical 

        // load image as Drawable
        drawable = Drawable.createFromStream(ims, null);

    }
    catch(IOException ex) {
        Log.d("LoadingImage", "Error reading the image");
        return null;
    }

    return drawable;
}

打开方法:

> public final InputStream open (String fileName, int accessMode)
> 
> Added in API level 1 Open an asset using an explicit access mode,
> returning an InputStream to read its contents. This provides access to
> files that have been bundled with an application as assets -- that is,
> files placed in to the "assets" directory.
> 
> fileName --- The name of the asset to open. This name can be hierarchical.
> 
> accessMode --- Desired access mode for retrieving the data.
> 
> Throws IOException

In the onCreate function:

RelativeLayout baseLayout = (RelativeLayout) this.findViewById(R.id.the_layout_id);

Drawable drawable = loadImageFromAsset();

if(drawable != null){
    baseLayout.setBackground(drawable);
    Log.d("TheActivity", "Setting the background");
}

The image loading method:

public Drawable loadImageFromAsset() {

    Drawable drawable;

    // load image
    try {

        // get input stream
        InputStream ims = getAssets().open("images/test.9.png");

        //Note: Images can be in hierarical 

        // load image as Drawable
        drawable = Drawable.createFromStream(ims, null);

    }
    catch(IOException ex) {
        Log.d("LoadingImage", "Error reading the image");
        return null;
    }

    return drawable;
}

The open method:

> public final InputStream open (String fileName, int accessMode)
> 
> Added in API level 1 Open an asset using an explicit access mode,
> returning an InputStream to read its contents. This provides access to
> files that have been bundled with an application as assets -- that is,
> files placed in to the "assets" directory.
> 
> fileName --- The name of the asset to open. This name can be hierarchical.
> 
> accessMode --- Desired access mode for retrieving the data.
> 
> Throws IOException
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文