Android:为RelativeLayout设置背景图片?

Viv*_*vek 15 android android-layout android-relativelayout

我在运行时下载了一个图像.现在我想将它设置为背景RelativeLayout.有可能吗?

Ste*_*teD 22

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

   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);
Run Code Online (Sandbox Code Playgroud)


小智 5

而是使用:

View lay = (View) findViewById(R.id.rLayout);
lay.setBackgroundResource(R.drawable.newImage);
Run Code Online (Sandbox Code Playgroud)

之所以有效,R.drawable.newImage是因为引用整数。因此,您可以执行以下操作:

int pic = R.drawable.newImage;
lay.setBackgroundResource(pic);
Run Code Online (Sandbox Code Playgroud)