我已經在運行時下載的圖像。現在我想將它設置爲相對佈局的背景如何?有可能嗎?爲相對佈局設置背景圖片?
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. Android:相對佈局的背景圖片
- 2. 如何動態設置相對佈局背景圖片?
- 3. 如何在相對佈局背景中設置位圖圖像
- 4. 如何設置不透明的圖片作爲佈局背景
- 5. 設置圖片爲背景
- 6. 擱置相對佈局背景
- 7. android相對佈局和圖像背景
- 8. 試圖設置一個相關佈局的背景圖像
- 9. 在mvc佈局頁面中設置背景圖片
- 10. Android:如何在表格佈局中設置背景圖片
- 11. 設置背景或背景圖片
- 12. 佈局佈局,頂部佈局的中心背景圖片
- 13. QDialog - 設置背景圖片
- 14. 設置背景圖片NSToolbar
- 15. 設置背景圖片
- 16. 設置背景圖片
- 17. Dropzone.js設置背景圖片
- 18. 設置Emacs背景圖片
- 19. 設置iOS背景圖片
- 20. 設置背景圖片
- 21. 設置背景圖片
- 22. UINavigationBar - 設置背景圖片?
- 23. 爲片段設置背景?
- 24. 佈局:拉伸的背景圖片
- 25. 如何適合佈局背景圖片
- 26. 將背景圖片設置爲QTextEdit
- 27. 爲QPushButton設置背景圖片
- 28. 如何爲Android設置背景圖片?
- 29. 如何爲IME設置背景圖片?
- 30. 爲tabbar設置背景圖片
正如我說我沒有圖像。我正在下載它,這意味着它不存在於可繪製文件夾中。是嗎? – Vivek 2011-02-06 08:05:43