2011-02-05 14 views

Respuesta

21

Eche un vistazo a setBackgroundDrawable, o quizás createFromPath en la clase 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); 
+0

como dije no tengo imagen. Lo estoy descargando, lo que significa que no se encuentra en una carpeta dibujable. ¿Lo es? – Vivek

+0

Sí, podría usar createFromPath para crear un objeto Drawable de la ruta donde residirá la imagen que acaba de descargar en – SteD

+1

El método 'setBackgroundDrawable (Drawable)' del tipo ** View ** está ** en desuso ** ... – Confuse

3

usar en su lugar:

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

Esto funciona porque R.drawable.newImage se refiere a un número entero. Por lo que podría hacer:

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

probar esto por Xamarin.Android (multiplataforma) -

RelativeLayout relativeLayout = new RelativeLayout (this); 

O

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

Y

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

En el onCreate función:

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"); 
} 

El método imagen de carga:

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; 
} 

El método abierto:

> 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 
Cuestiones relacionadas