2011-02-05 67 views

回答

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); 
+0

正如我说我没有图像。我正在下载它,这意味着它不存在于可绘制文件夹中。是吗? – Vivek 2011-02-06 08:05:43

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