我已经在运行时下载的图像。现在我想将它设置为相对布局的背景如何?有可能吗?为相对布局设置背景图片?
15
A
回答
21
退房的setBackgroundDrawable,也许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);
3
而是使用:
View lay = (View) findViewById(R.id.rLayout);
lay.setBackgroundResource(R.drawable.newImage);
这工作,因为R.drawable.newImage
是指一个整数。所以,你可以这样做:
int pic = R.drawable.newImage;
lay.setBackgroundResource(pic);
2
尝试此Xamarin.Android(跨平台) -
RelativeLayout relativeLayout = new RelativeLayout (this);
OR
RelativeLayout relativeLayout = (RelativeLayout)FindViewById (Resource.Id.relativeLayout);
和
relativeLayout.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.imageName));
0
在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;
}
open方法:
> 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
相关问题
- 1. 如何动态设置相对布局背景图片?
- 2. 如何在相对布局背景中设置位图图像
- 3. 设置图片为背景
- 4. android相对布局和图像背景
- 5. 试图设置一个相关布局的背景图像
- 6. 在mvc布局页面中设置背景图片
- 7. 设置背景或背景图片
- 8. 布局布局,顶部布局的中心背景图片
- 9. 设置背景图片
- 10. QDialog - 设置背景图片
- 11. Dropzone.js设置背景图片
- 12. 设置Emacs背景图片
- 13. UINavigationBar - 设置背景图片?
- 14. 设置背景图片
- 15. 设置背景图片
- 16. 如何适合布局背景图片
- 17. 为片段设置背景?
- 18. 为QPushButton设置背景图片
- 19. 如何为IME设置背景图片?
- 20. 设置背景的视图中使用相对布局编程xamarin形式
- 21. Android:将布局设置为视图的背景
- 22. android - 从url设置布局背景
- 23. 如何将画布的大小设置为其背景图片?
- 24. 如何将相对布局设置为默认布局
- 25. Android:如何为布局选择背景图片
- 26. 如何使用Volley或其他设置从URL设置相对布局背景图像?
- 27. 设置“默认”UIViewController背景图片?
- 28. NativeScript:如何设置背景图片?
- 29. 设置背景图片可以点击
- 30. 无法设置背景图片
正如我说我没有图像。我正在下载它,这意味着它不存在于可绘制文件夹中。是吗? – Vivek 2011-02-06 08:05:43